Move G3 repeater policy tables and MQTT WebSocket buffers to PSRAM

This commit is contained in:
mikecarper committed 2026-10-27 13:14:28 -07:00
1 parent a6d472ff9d
commit 74c03cb30f
15 files changed
+1856 -7

No files matched your search

+143
View File
@@ -453,6 +453,149 @@ int main() {
sensor = ram.requirements("NRF52_PLATFORM", {}, "t114_sensor")
self.assertEqual(sensor["components"]["client_table"], 32 * 320 + 16)
def test_g3_psram_tables_reserve_full_internal_fallback_without_cutting_other_owners(self):
flags = {"STATION_G3_ESP32": 1, "BOARD_HAS_PSRAM": 1,
"MESH_CLIENT_REPEATER_ONLY": 1, "WITH_MQTT_BRIDGE": 1,
"DISPLAY_CLASS": "SH1106Display", "ADMIN_PASSWORD": "test",
"ENABLE_OTA": 1, "WITH_UDP_BRIDGE": 1, "WITH_RS232_BRIDGE": "Serial2"}
target = "Station_G3_ESP32_repeater"
original = ram.requirements("ESP32_PLATFORM", {
**flags, "MESH_REPEATER_PSRAM_TABLES": 0}, target)
selected = ram.requirements("ESP32_PLATFORM", flags, target)
scope = 255 * 36 + 255 * 34 + 32 * 32 + 2 + 16
advert = 128 * 140 + 16
self.assertEqual(selected["components"], {
**original["components"], "repeater_scope_storage_fallback": scope,
"repeater_advert_storage_fallback": advert})
self.assertEqual(selected["required_heap_bytes"],
original["required_heap_bytes"] + scope + advert)
self.assertEqual(selected["mqtt"], original["mqtt"])
self.assertEqual(selected["components"]["allocation_and_transient_margin"], 16384)
self.assertEqual(selected["components"]["wireless_stacks"], 49152)
self.assertEqual(ram.requirements("ESP32_PLATFORM", {
**flags, "MESH_REPEATER_PSRAM_TABLES": 1}, target), selected)
def test_g3_psram_table_defaults_follow_board_psram_platform_and_source_role_gates(self):
flags = {"STATION_G3_ESP32": 1, "BOARD_HAS_PSRAM": 1,
"MESH_CLIENT_REPEATER_ONLY": 1}
cases = [
("ESP32_PLATFORM", {k: v for k, v in flags.items() if k != "STATION_G3_ESP32"},
"other_s3_repeater"),
("ESP32_PLATFORM", {k: v for k, v in flags.items() if k != "BOARD_HAS_PSRAM"},
"Station_G3_ESP32_repeater"),
("ESP32_PLATFORM", {k: v for k, v in flags.items() if k != "MESH_CLIENT_REPEATER_ONLY"},
"Station_G3_ESP32_repeater"),
("ESP32_PLATFORM", {**flags, "MESH_CLIENT_REPEATER_ONLY": 0},
"Station_G3_ESP32_repeater"),
("ESP32_PLATFORM", {**flags, "MESH_CLIENT_REPEATER_ONLY": 0,
"COMPANION_RADIO_FULL": 1},
"Station_G3_ESP32_companion_radio_full"),
("ESP32_PLATFORM", {**flags, "MESH_CLIENT_REPEATER_ONLY": 0},
"Station_G3_ESP32_room_server"),
]
for platform in ("NRF52_PLATFORM", "RP2040_PLATFORM", "STM32_PLATFORM"):
cases.extend((platform, {**flags, **extra}, "other_repeater")
for extra in ({}, {"MESH_REPEATER_PSRAM_TABLES": 1}))
for platform, defines, target in cases:
with self.subTest(platform=platform, defines=defines, target=target):
policy = ram.requirements(platform, defines, target)
self.assertNotIn("repeater_scope_storage_fallback", policy["components"])
self.assertNotIn("repeater_advert_storage_fallback", policy["components"])
self.assertEqual(policy, ram.requirements(platform, {
**defines, "MESH_REPEATER_PSRAM_TABLES": 0}, target))
# The source role, rather than a release filename, selects MyMesh.
renamed = ram.requirements("ESP32_PLATFORM", flags, "custom_board_forwarder")
self.assertIn("repeater_scope_storage_fallback", renamed["components"])
self.assertIn("repeater_advert_storage_fallback", renamed["components"])
def test_explicit_psram_repeater_table_opt_in_and_capacity_overrides_are_fully_budgeted(self):
flags = {"BOARD_HAS_PSRAM": 1, "MESH_CLIENT_REPEATER_ONLY": 1,
"MESH_REPEATER_PSRAM_TABLES": 1}
cases = (
({}, 255, 255, 32, 128),
({"FLOOD_CHANNEL_SCOPE_SLOTS": 5, "MAX_REGION_ENTRIES": 2}, 5, 5, 2, 128),
({"FLOOD_CHANNEL_SCOPE_SLOTS": 7, "FLOOD_CHANNEL_SCOPE_REQUIRE_SLOTS": 11,
"FLOOD_CHANNEL_DIRECT_SCOPE_SLOTS": 3, "FLOOD_ADVERT_SOURCE_SLOTS": 17},
7, 11, 3, 17),
({"FLOOD_CHANNEL_SCOPE_SLOTS": 3, "MAX_REGION_ENTRIES": 2,
"FLOOD_CHANNEL_DIRECT_SCOPE_SLOTS": 9}, 3, 3, 9, 128),
({"MAX_REGION_ENTRIES": 64, "FLOOD_ADVERT_SOURCE_SLOTS": 200},
255, 255, 64, 200),
)
for extra, scopes, required, direct, adverts in cases:
with self.subTest(extra=extra):
defines = {**flags, **extra}
selected = ram.requirements("ESP32_PLATFORM", defines, "other_s3_repeater")
original = ram.requirements("ESP32_PLATFORM", {
**defines, "MESH_REPEATER_PSRAM_TABLES": 0}, "other_s3_repeater")
scope = scopes * 36 + required * 34 + direct * 32 + 2 + 16
advert = adverts * 140 + 16
self.assertEqual(selected["components"], {
**original["components"], "repeater_scope_storage_fallback": scope,
"repeater_advert_storage_fallback": advert})
self.assertEqual(selected["required_heap_bytes"],
original["required_heap_bytes"] + scope + advert)
def test_psram_advert_budget_excludes_portable_iram_and_rtc_storage_only(self):
flags = {"STATION_G3_ESP32": 1, "BOARD_HAS_PSRAM": 1,
"MESH_CLIENT_REPEATER_ONLY": 1}
target = "Station_G3_ESP32_repeater"
original = ram.requirements("ESP32_PLATFORM", flags, target)
advert = original["components"]["repeater_advert_storage_fallback"]
for extra in ({"PORTABLE_MQTT_OBSERVER": 1}, {"PORTABLE_MQTT_OBSERVER": 0},
{"FLOOD_ADVERT_IRAM_WORD_STORAGE": 1},
{"FLOOD_ADVERT_RTC_FAST_SLOTS": 16},
{"FLOOD_ADVERT_RTC_SLOW_SLOTS": 50},
{"FLOOD_ADVERT_RTC_FAST_SLOTS": 50, "FLOOD_ADVERT_RTC_SLOW_SLOTS": 50}):
with self.subTest(extra=extra):
policy = ram.requirements("ESP32_PLATFORM", {**flags, **extra}, target)
self.assertEqual(policy["components"], {
k: v for k, v in original["components"].items()
if k != "repeater_advert_storage_fallback"})
self.assertEqual(policy["required_heap_bytes"],
original["required_heap_bytes"] - advert)
disabled_backends = {"FLOOD_ADVERT_IRAM_WORD_STORAGE": 0,
"FLOOD_ADVERT_RTC_FAST_SLOTS": 0, "FLOOD_ADVERT_RTC_SLOW_SLOTS": 0}
self.assertEqual(ram.requirements("ESP32_PLATFORM", {
**flags, **disabled_backends}, target), original)
def test_each_psram_table_fallback_can_set_the_contiguous_allocation_bound(self):
flags = {"STATION_G3_ESP32": 1, "BOARD_HAS_PSRAM": 1,
"MESH_CLIENT_REPEATER_ONLY": 1, "MAX_CLIENTS": 1,
"FLOOD_PACKET_FILTER_SLOTS": 1, "MAX_RECENT_REPEATERS": 0}
for extra, name in (({}, "repeater_scope_storage_fallback"),
({"FLOOD_ADVERT_SOURCE_SLOTS": 200},
"repeater_advert_storage_fallback")):
with self.subTest(extra=extra):
policy = ram.requirements("ESP32_PLATFORM", {**flags, **extra}, "g3_repeater")
self.assertEqual(policy["required_contiguous_bytes"], policy["components"][name])
self.assertGreater(policy["required_contiguous_bytes"], 16384)
def test_g3_psram_fallback_elf_gate_rejects_old_budget_and_enforces_exact_total(self):
flags = {"STATION_G3_ESP32": 1, "BOARD_HAS_PSRAM": 1,
"MESH_CLIENT_REPEATER_ONLY": 1, "WITH_MQTT_BRIDGE": 1}
target = "Station_G3_ESP32_repeater"
previous = {**flags, "MESH_REPEATER_PSRAM_TABLES": 0}
old = ram.requirements("ESP32_PLATFORM", previous, target)["required_heap_bytes"]
required = ram.requirements("ESP32_PLATFORM", flags, target)["required_heap_bytes"]
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary) / "firmware.elf"
report = Path(temporary) / "firmware.memory.json"
for capacity, accepted in ((old, False), (required - 1, False), (required, True)):
with self.subTest(capacity=capacity), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
esp_fixture(path, modern=True, internal_length=capacity + 0x4000)
self.assertEqual(ram.check_firmware(path, "ESP32_PLATFORM", "esp32s3",
previous, target), 0)
status = ram.check_firmware(path, "ESP32_PLATFORM", "esp32s3",
flags, target, report)
result = json.loads(report.read_text())
self.assertEqual(status == 0, accepted)
self.assertEqual(result["passed"], accepted)
self.assertEqual(result["available_internal_bytes"], capacity)
self.assertEqual(result["required_heap_bytes"], required)
self.assertEqual(result["components"]["repeater_scope_storage_fallback"], 18892)
self.assertEqual(result["components"]["repeater_advert_storage_fallback"], 17936)
def test_browser_terminal_reserves_internal_session_and_psram_aware_scrollback(self):
defines = {"ENABLE_USB_INTERFACE": 1, "WIFI_SSID": "", "DISPLAY_CLASS": "SSD1306Display"}
base = ram.requirements("ESP32_PLATFORM", {**defines, "WEBCONFIG_DISABLED": 1}, "v4_companion")
+209
View File
@@ -0,0 +1,209 @@
"""Keep every advert-history decision while moving its complete table to PSRAM."""
from pathlib import Path
import os
import resource
import signal
import subprocess
import tempfile
import unittest
from test_flood_advert_rtc import HARNESS
ROOT = Path(__file__).resolve().parents[1]
HEAP = r'''
#pragma once
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <map>
#define HEAP_CHECK(value) do { if (!(value)) std::abort(); } while (0)
enum { MALLOC_CAP_INTERNAL=1, MALLOC_CAP_DMA=2,
MALLOC_CAP_SPIRAM=4, MALLOC_CAP_8BIT=8 };
namespace heap_test {
struct Block { size_t bytes; unsigned caps; };
struct Heap {
bool psram = true, internal = true;
size_t dma_free = 100000, dma_largest = 100000;
unsigned external_calls = 0, internal_calls = 0, frees = 0;
size_t last_bytes = 0;
std::map<void*, Block> blocks;
};
inline Heap& heap() { static Heap value; return value; }
}
inline void* heap_caps_malloc(size_t size, unsigned caps) {
using namespace heap_test;
Heap& state = heap();
state.last_bytes = size;
const bool external = (caps & MALLOC_CAP_SPIRAM) != 0;
if (external) ++state.external_calls; else ++state.internal_calls;
if (external ? !state.psram : !state.internal) return nullptr;
if (!external) {
HEAP_CHECK(caps & MALLOC_CAP_INTERNAL);
HEAP_CHECK(size <= state.dma_largest && size <= state.dma_free - 16384);
state.dma_free -= size;
}
void* memory = std::malloc(size);
HEAP_CHECK(memory);
std::memset(memory, 0xa5, size);
state.blocks[memory] = {size, caps};
return memory;
}
inline void* heap_caps_calloc(size_t count, size_t size, unsigned caps) {
void* memory = heap_caps_malloc(count * size, caps);
if (memory) std::memset(memory, 0, count * size);
return memory;
}
inline void heap_caps_free(void* memory) {
if (!memory) return;
using namespace heap_test;
Heap& state = heap();
const auto found = state.blocks.find(memory);
HEAP_CHECK(found != state.blocks.end());
if (!(found->second.caps & MALLOC_CAP_SPIRAM))
state.dma_free += found->second.bytes;
state.blocks.erase(found);
++state.frees;
std::free(memory);
}
inline size_t heap_caps_get_free_size(unsigned caps) {
HEAP_CHECK(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
return heap_test::heap().dma_free;
}
inline size_t heap_caps_get_largest_free_block(unsigned caps) {
HEAP_CHECK(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
return heap_test::heap().dma_largest;
}
'''
WRAPPER = r'''
int main(int argc, char** argv) {
auto& heap = heap_test::heap();
const char* mode = argc > 1 ? argv[1] : "trace";
const bool fallback = !std::strcmp(mode, "fallback");
const bool lifetime = !std::strcmp(mode, "lifetime");
if (fallback || !std::strcmp(mode, "no-memory")
|| !std::strcmp(mode, "reserve") || !std::strcmp(mode, "fragmented"))
heap.psram = false;
if (!std::strcmp(mode, "no-memory")) heap.internal = false;
if (!std::strcmp(mode, "reserve")) heap.dma_free = 16384 + 17920 - 1;
if (!std::strcmp(mode, "fragmented")) heap.dma_largest = 17920 - 1;
char trace[] = "trace";
char life[] = "lifetime";
char* args[] = {argv[0], lifetime ? life : trace, nullptr};
const size_t dma_before = heap.dma_free;
const int result = traceMain(2, args);
CHECK(heap.blocks.empty() && heap.dma_free == dma_before);
#if MESH_REPEATER_PSRAM_TABLES
CHECK(heap.last_bytes == 128 * sizeof(mesh::FloodAdvertLimiter::Entry));
CHECK(heap.external_calls == (lifetime ? 3u : 1u));
CHECK(heap.internal_calls == (fallback ? 1u : 0u));
CHECK(heap.frees == (lifetime ? 3u : 1u));
#else
CHECK(heap.external_calls == 0 && heap.internal_calls == 0 && heap.frees == 0);
#endif
return result;
}
'''
class FloodAdvertPsramTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory(prefix="advert-psram-")
cls.work = Path(cls.temp.name)
(cls.work / "esp_heap_caps.h").write_text(HEAP)
(cls.work / "sdkconfig.h").write_text(
"#define CONFIG_IDF_TARGET_ESP32S3 1\n"
"#define CONFIG_SPIRAM_BOOT_INIT 1\n"
"#define CONFIG_SPIRAM_USE_MALLOC 1\n")
cls.compiler = [os.environ.get("CXX", "g++")]
cls.binaries = {}
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def build(self, enabled, collisions=False):
key = (enabled, collisions)
if key in self.binaries:
return self.binaries[key]
name = "probe-%d-%d" % key
source = self.work / (name + ".cpp")
binary = self.work / name
code = HARNESS.replace("@BANKS@", "").replace("int main(", "int traceMain(")
# Release builds of the limiter are exercised while harness assertions
# stay active. The trace also checks exact full-key prefix collisions.
code = code.rstrip()[:-1] + " return 0;\n}\n"
if collisions:
code = code.replace("key[0] = uint8_t(i); key[1] = uint8_t(i >> 8);",
"key[0] = uint8_t(i / 2); key[1] = uint8_t(i >> 8);")
source.write_text("#include <esp_heap_caps.h>\n" + code + WRAPPER)
args = ["-std=c++11", "-Os", "-DNDEBUG", "-Wall", "-Wextra", "-Werror",
"-fsanitize=address,undefined", "-fno-sanitize-recover=all",
"-fno-pie", "-no-pie", "-DESP32_PLATFORM=1", "-DESP_PLATFORM=1",
"-DBOARD_HAS_PSRAM=1", "-DSTATION_G3_ESP32=1",
"-DMESH_CLIENT_REPEATER_ONLY=1",
"-DMESH_REPEATER_PSRAM_TABLES=" + str(enabled),
"-I", str(self.work), "-isystem", str(ROOT / "src"),
"-isystem", str(ROOT / "test/mocks")]
result = subprocess.run(
self.compiler + args + [str(source),
str(ROOT / "src/helpers/FloodAdvertLimiter.cpp"), "-o", str(binary)],
capture_output=True, text=True, timeout=30)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.binaries[key] = binary
return binary
def run_probe(self, enabled, mode="trace", collisions=False):
return subprocess.run([str(self.build(enabled, collisions)), mode],
capture_output=True, text=True, timeout=20)
def test_full_capacity_quota_duplicate_badlist_and_rollover_traces_match(self):
for collisions in (False, True):
with self.subTest(collisions=collisions):
baseline = self.run_probe(0, collisions=collisions)
self.assertEqual(baseline.returncode, 0, baseline.stderr)
self.assertIn("limited=128", baseline.stdout)
for mode in ("trace", "fallback"):
result = self.run_probe(1, mode, collisions)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, baseline.stdout)
def test_owner_recreation_clears_history_and_releases_every_allocation(self):
baseline = self.run_probe(0, "lifetime")
result = self.run_probe(1, "lifetime")
self.assertEqual(baseline.returncode, 0, baseline.stderr)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, baseline.stdout)
def test_exhausted_or_reserved_internal_memory_stops_startup(self):
def no_core():
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
binary = self.build(1)
for mode in ("no-memory", "reserve", "fragmented"):
with self.subTest(mode=mode):
result = subprocess.run([str(binary), mode], capture_output=True,
text=True, preexec_fn=no_core, timeout=20)
self.assertEqual(result.returncode, -signal.SIGABRT, result.stderr)
self.assertIn("Cannot allocate advert source history (17920 bytes)",
result.stderr)
self.assertNotIn("runtime error:", result.stderr)
self.assertNotIn("AddressSanitizer", result.stderr)
def test_selected_owner_retains_no_full_internal_fallback(self):
source = self.work / "layout.cpp"
source.write_text('#include <helpers/FloodAdvertLimiter.h>\n'
'static_assert(sizeof(mesh::StaticFloodAdvertLimiter<>) < 256, '
'"advert owner must not retain a full inline table");\n')
result = subprocess.run(self.compiler + ["-std=c++11", "-fsyntax-only",
"-DESP32_PLATFORM=1", "-DESP_PLATFORM=1", "-DBOARD_HAS_PSRAM=1",
"-DSTATION_G3_ESP32=1", "-DMESH_CLIENT_REPEATER_ONLY=1",
"-DMESH_REPEATER_PSRAM_TABLES=1", "-I", str(self.work),
"-isystem", str(ROOT / "src"), "-isystem", str(ROOT / "test/mocks"),
str(source)], capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
if __name__ == "__main__":
unittest.main()
+375
View File
@@ -0,0 +1,375 @@
"""Exercise actual scope table owners, persistence and rollback in both RAM backends."""
from pathlib import Path
import os
import resource
import signal
import subprocess
import tempfile
import unittest
from cpp_source import body
from test_flood_advert_psram import HEAP
ROOT = Path(__file__).resolve().parents[1]
SDK = ("#define CONFIG_IDF_TARGET_ESP32S3 1\n"
"#define CONFIG_SPIRAM_BOOT_INIT 1\n"
"#define CONFIG_SPIRAM_USE_MALLOC 1\n")
DEFINES = ["ESP32_PLATFORM=1", "BOARD_HAS_PSRAM=1", "STATION_G3_ESP32=1",
"MESH_CLIENT_REPEATER_ONLY=1"]
PRELUDE = r'''
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <esp_heap_caps.h>
#include <helpers/RepeaterPsramStorage.h>
#include <helpers/FloodFilterPolicy.h>
#define CHECK(value) do { if (!(value)) std::abort(); } while (0)
#define FLOOD_CHANNEL_SCOPE_SLOTS 255
#define FLOOD_CHANNEL_DIRECT_SCOPE_SLOTS 32
#define FLOOD_CHANNEL_SCOPE_REQUIRE_SLOTS 255
#define FLOOD_PACKET_FILTER_SCOPE_NAME_LEN 32
#define FLOOD_CHANNEL_SCOPE_TXT_ANY 0
#define FLOOD_CHANNEL_SCOPE_OTHER_ANY 2
#define MESH_ENABLE_FLOOD_RULE_ENGINE 0
#define FLOOD_CHANNEL_SCOPE_FILE "/scopes"
#define FLOOD_CHANNEL_SCOPE_TEMP_FILE "/scopes.tmp"
#define FLOOD_CHANNEL_SCOPE_REQUIRE_FILE "/require"
#define FLOOD_CHANNEL_SCOPE_REQUIRE_TEMP_FILE "/require.tmp"
struct RegionMap {
static bool is_name_char(uint8_t c) { return std::isalnum(c) || c == '_' || c == '-'; }
};
namespace mesh { struct Utils {
static void sha256(void* out, size_t size, const uint8_t*, size_t length) {
CHECK(size == 1 && (length == 16 || length == 32));
std::memset(out, 0, size);
}
}; }
static unsigned external_io = 0;
static void inspectSpan(const void* data, size_t size) {
const uintptr_t address = reinterpret_cast<uintptr_t>(data);
for (const auto& block : heap_test::heap().blocks) {
const uintptr_t start = reinterpret_cast<uintptr_t>(block.first);
if (address >= start && address < start + block.second.bytes) {
CHECK(size <= start + block.second.bytes - address);
if (block.second.caps & MALLOC_CAP_SPIRAM) ++external_io;
}
}
}
struct MemoryFS;
struct File {
MemoryFS* fs = nullptr;
std::string path;
size_t cursor = 0;
bool valid = false;
explicit operator bool() const { return valid; }
size_t read(uint8_t*, size_t);
size_t write(const uint8_t*, size_t);
void close() { valid = false; }
};
struct MemoryFS {
std::map<std::string, std::vector<uint8_t>> files;
bool fail_rename = false, short_write = false;
bool exists(const char* path) { return files.count(path); }
bool remove(const char* path) { return files.erase(path); }
bool rename(const char* from, const char* to) {
if (fail_rename || !files.count(from)) return false;
files[to] = files[from]; files.erase(from); return true;
}
};
size_t File::read(uint8_t* out, size_t size) {
if (!valid) return 0;
const auto& bytes = fs->files[path];
size = std::min(size, bytes.size() - cursor);
inspectSpan(out, size);
if (size) std::memcpy(out, bytes.data() + cursor, size);
cursor += size; return size;
}
size_t File::write(const uint8_t* data, size_t size) {
if (!valid) return 0;
if (fs->short_write && size) --size;
inspectSpan(data, size);
auto& bytes = fs->files[path];
bytes.insert(bytes.end(), data, data + size); return size;
}
static File openFloodSettingsRead(MemoryFS* fs, const char* path) {
return File{fs, path, 0, fs->exists(path)};
}
static File openFloodSettingsWrite(MemoryFS* fs, const char* path) {
fs->files[path].clear(); return File{fs, path, 0, true};
}
'''
SCENARIOS = r'''
static std::vector<uint8_t> snapshot(const MyMesh& mesh) {
std::vector<uint8_t> bytes;
const auto add = [&](const void* data, size_t size) {
const auto first = static_cast<const uint8_t*>(data);
bytes.insert(bytes.end(), first, first + size);
};
add(mesh.flood_channel_scopes, sizeof(mesh.flood_channel_scopes));
add(mesh.flood_channel_direct_scopes, sizeof(mesh.flood_channel_direct_scopes));
add(mesh.flood_channel_scope_requirements, sizeof(mesh.flood_channel_scope_requirements));
return bytes;
}
static void populate(MyMesh& mesh) {
for (unsigned i = 0; i < 32; ++i)
std::snprintf(mesh.flood_channel_direct_scopes[i], 32, "#Scope%u", i);
for (unsigned i = 0; i < 255; ++i) {
auto& row = mesh.flood_channel_scopes[i];
row.target_id = i % 32 + 1;
row.selector = FloodFilterPolicy::encodeChannelScopeTargetSelector(0, true);
auto& requirement = mesh.flood_channel_scope_requirements[i];
requirement.key_len = 32;
std::memset(requirement.secret, uint8_t(i + 1), sizeof(requirement.secret));
}
}
static void exercise(const char* mode) {
MemoryFS disk;
{
MyMesh writer(disk);
static_assert(sizeof(writer.flood_channel_scopes) == 255 * 36, "all scope slots");
static_assert(sizeof(writer.flood_channel_direct_scopes) == 32 * 32, "all direct names");
static_assert(sizeof(writer.flood_channel_scope_requirements) == 255 * 34, "all requirements");
#if MESH_REPEATER_PSRAM_TABLES
static_assert(sizeof(MyMesh) < 256, "no inline fallback arrays");
auto& heap = heap_test::heap();
CHECK(heap.blocks.size() == 1);
CHECK(heap.last_bytes == 18874);
const auto caps = heap.blocks.begin()->second.caps;
CHECK(caps == (!std::strcmp(mode, "fallback")
? (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
: (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)));
const unsigned calls = heap.external_calls + heap.internal_calls;
CHECK(writer.flood_channel_storage.begin());
CHECK(heap.external_calls + heap.internal_calls == calls);
#else
CHECK(heap_test::heap().blocks.empty());
(void)mode;
#endif
CHECK(snapshot(writer) == std::vector<uint8_t>(18874, 0));
const void* scopes = writer.flood_channel_scopes;
const void* direct = writer.flood_channel_direct_scopes;
const void* require = writer.flood_channel_scope_requirements;
populate(writer);
const auto initial = snapshot(writer);
CHECK(writer.saveFloodChannelScopes());
CHECK(writer.saveFloodChannelScopeRequirements());
CHECK(disk.files["/scopes"].size() == 10210);
CHECK(disk.files["/require"].size() == 8675);
std::vector<uint8_t> expected = {'F','C','S','5',255};
const auto* start = reinterpret_cast<const uint8_t*>(writer.flood_channel_scopes);
expected.insert(expected.end(), start, start + 9180);
expected.push_back(32);
start = reinterpret_cast<const uint8_t*>(writer.flood_channel_direct_scopes);
expected.insert(expected.end(), start, start + 1024);
CHECK(disk.files["/scopes"] == expected);
expected = {'F','C','R','1',255};
start = reinterpret_cast<const uint8_t*>(writer.flood_channel_scope_requirements);
expected.insert(expected.end(), start, start + 8670);
CHECK(disk.files["/require"] == expected);
const auto saved = disk.files;
char reply[160];
for (bool short_write : {false, true}) {
disk.fail_rename = !short_write; disk.short_write = short_write;
writer.deleteFloodChannelScope(".255", reply);
CHECK(!std::strcmp(reply, "Err - save failed"));
CHECK(snapshot(writer) == initial && disk.files == saved);
writer.deleteFloodChannelScopeRequirement(".255", reply);
CHECK(!std::strcmp(reply, "Err - save failed"));
CHECK(snapshot(writer) == initial && disk.files == saved);
writer.deleteFloodChannelScope("all", reply);
CHECK(!std::strcmp(reply, "Err - save failed"));
CHECK(snapshot(writer) == initial && disk.files == saved);
writer.deleteFloodChannelScopeRequirement("all", reply);
CHECK(!std::strcmp(reply, "Err - save failed"));
CHECK(snapshot(writer) == initial && disk.files == saved);
}
disk.fail_rename = disk.short_write = false;
std::memset(writer.flood_channel_scopes, 0xa5, sizeof(writer.flood_channel_scopes));
std::memset(writer.flood_channel_direct_scopes, 0xa5, sizeof(writer.flood_channel_direct_scopes));
std::memset(writer.flood_channel_scope_requirements, 0xa5, sizeof(writer.flood_channel_scope_requirements));
CHECK(writer.loadFloodChannelScopes());
writer.loadFloodChannelScopeRequirements();
CHECK(snapshot(writer) == initial);
disk.files["/scopes"].pop_back();
disk.files["/require"].pop_back();
CHECK(!writer.loadFloodChannelScopes());
writer.loadFloodChannelScopeRequirements();
CHECK(snapshot(writer) == std::vector<uint8_t>(18874, 0));
disk.files = saved;
CHECK(writer.loadFloodChannelScopes());
writer.loadFloodChannelScopeRequirements();
CHECK(snapshot(writer) == initial);
CHECK(scopes == writer.flood_channel_scopes && direct == writer.flood_channel_direct_scopes
&& require == writer.flood_channel_scope_requirements);
// Compare the complete production file payloads between inline and PSRAM runs.
for (const char* path : {"/scopes", "/require"}) {
const auto& bytes = disk.files[path];
CHECK(std::fwrite(bytes.data(), 1, bytes.size(), stdout) == bytes.size());
}
writer.deleteFloodChannelScope(".255", reply);
CHECK(!std::strcmp(reply, "OK") && writer.flood_channel_scopes[254].target_id == 0);
writer.deleteFloodChannelScopeRequirement(".255", reply);
CHECK(!std::strcmp(reply, "OK") && writer.flood_channel_scope_requirements[254].key_len == 0);
writer.deleteFloodChannelScope("all", reply);
CHECK(!std::strcmp(reply, "OK"));
writer.deleteFloodChannelScopeRequirement("all", reply);
CHECK(!std::strcmp(reply, "OK"));
CHECK(snapshot(writer) == std::vector<uint8_t>(18874, 0));
}
CHECK(heap_test::heap().blocks.empty());
MyMesh recreated(disk);
CHECK(snapshot(recreated) == std::vector<uint8_t>(18874, 0));
}
int main(int argc, char** argv) {
const char* mode = argc > 1 ? argv[1] : "trace";
auto& heap = heap_test::heap();
if (std::strcmp(mode, "trace")) heap.psram = false;
if (!std::strcmp(mode, "no-memory")) heap.internal = false;
if (!std::strcmp(mode, "reserve")) heap.dma_free = 16384 + 18874 - 1;
if (!std::strcmp(mode, "fragmented")) heap.dma_largest = 18874 - 1;
if (!std::strcmp(mode, "boundary")) {
heap.dma_free = 16384 + 18874;
heap.dma_largest = 18874;
mode = "fallback";
}
const size_t free_before = heap.dma_free;
exercise(mode);
CHECK(heap.blocks.empty() && heap.dma_free == free_before);
#if MESH_REPEATER_PSRAM_TABLES
CHECK(heap.external_calls == 2 && heap.frees == 2);
CHECK(heap.internal_calls == (!std::strcmp(mode, "fallback") ? 2u : 0u));
CHECK(external_io > 0 || !std::strcmp(mode, "fallback"));
#else
CHECK(heap.external_calls == 0 && heap.internal_calls == 0 && heap.frees == 0);
#endif
}
'''
def production_program():
header = (ROOT / "examples/simple_repeater/MyMesh.h").read_text(encoding="ascii")
source = (ROOT / "examples/simple_repeater/MyMesh.cpp").read_text(encoding="ascii")
location = header.index(" struct FloodChannelStorage {")
start = header.rfind("#if MESH_REPEATER_PSRAM_TABLES", 0, location)
declarations = header[start:header.index("#if MESH_ENABLE_FLOOD_GROUP_MODERATION", location)]
constructor = source.index("MyMesh::MyMesh(")
start = source.index("#if MESH_REPEATER_PSRAM_TABLES", constructor)
binders = source[start:source.index("#endif", start) + len("#endif")]
start = source.index(" memset(flood_channel_scopes, 0, sizeof(flood_channel_scopes));", constructor)
resets = source[start:source.index("#if MESH_ENABLE_FLOOD_GROUP_MODERATION", start)]
methods = [
"bool MyMesh::loadFloodChannelScopes(", "bool MyMesh::saveFloodChannelScopes(",
"void MyMesh::loadFloodChannelScopeRequirements(",
"bool MyMesh::saveFloodChannelScopeRequirements(",
"void MyMesh::deleteFloodChannelScope(",
"void MyMesh::deleteFloodChannelScopeRequirement(",
]
helpers = [
"static const char* skipFloodFilterSpaces(", "static bool floodFilterAsciiEqual(",
"static bool parseFloodFilterUnsigned(", "static bool parseFloodChannelScopeIndex(",
"static bool isValidStoredFloodFilterScopeName(",
"static bool isExactFloodChannelScopeSelector(",
]
owner = "class MyMesh {\npublic:\nMemoryFS* _fs;\n"
for name in ("FloodChannelScopeEntry", "FloodChannelScopeRequireEntry"):
owner += body(header, "struct " + name + " {") + ";\n"
owner += declarations + "\nint end_marker;\n"
owner += "explicit MyMesh(MemoryFS& fs) : _fs(&fs),\n" + binders
owner += "\nend_marker(0) {\n" + resets + "}\n"
owner += ("bool loadFloodChannelScopes();\n"
"bool saveFloodChannelScopes(bool empty_table=false);\n"
"void loadFloodChannelScopeRequirements();\n"
"bool saveFloodChannelScopeRequirements(bool empty_table=false);\n"
"void deleteFloodChannelScope(const char*, char*);\n"
"void deleteFloodChannelScopeRequirement(const char*, char*);\n};\n")
return "\n".join([PRELUDE, owner] + [body(source, item) for item in helpers + methods] + [SCENARIOS])
class RepeaterScopePsramTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temporary = tempfile.TemporaryDirectory(prefix="repeater-scope-psram-")
cls.work = Path(cls.temporary.name)
(cls.work / "esp_heap_caps.h").write_text(HEAP, encoding="ascii")
(cls.work / "sdkconfig.h").write_text(SDK, encoding="ascii")
(cls.work / "scope.cpp").write_text(production_program(), encoding="ascii")
cls.compiler = [os.environ.get("CXX", "g++")]
cls.binaries = {}
@classmethod
def tearDownClass(cls):
cls.temporary.cleanup()
def build(self, enabled):
if enabled not in self.binaries:
binary = self.work / ("scope-%d" % enabled)
command = self.compiler + ["-std=c++17", "-O1", "-g", "-DNDEBUG",
"-Wall", "-Wextra", "-Werror", "-Wno-sign-compare",
"-fsanitize=address,undefined", "-fno-sanitize-recover=all",
"-fno-pie", "-no-pie", "-I", str(self.work),
"-isystem", str(ROOT / "src"), "-isystem", str(ROOT / "test/mocks")]
command += ["-D" + define for define in DEFINES]
command += ["-DMESH_REPEATER_PSRAM_TABLES=%d" % enabled,
str(self.work / "scope.cpp"), "-o", str(binary)]
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.binaries[enabled] = binary
return self.binaries[enabled]
def test_full_table_file_bytes_loads_resets_lifetime_and_rollback_match(self):
baseline = subprocess.run([str(self.build(0))], capture_output=True, timeout=20)
self.assertEqual(baseline.returncode, 0, baseline.stderr.decode())
self.assertEqual(len(baseline.stdout), 18885)
for mode in ("trace", "fallback", "boundary"):
with self.subTest(mode=mode):
result = subprocess.run([str(self.build(1)), mode], capture_output=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr.decode())
self.assertEqual(result.stdout, baseline.stdout)
def test_no_capacity_reduction_or_null_references_on_allocation_failure(self):
def no_core():
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
for mode in ("no-memory", "reserve", "fragmented"):
with self.subTest(mode=mode):
result = subprocess.run([str(self.build(1)), mode], capture_output=True,
text=True, timeout=20, preexec_fn=no_core)
self.assertEqual(result.returncode, -signal.SIGABRT, result.stderr)
self.assertIn("Cannot allocate channel scope tables (18874 bytes)", result.stderr)
self.assertNotIn("runtime error:", result.stderr)
self.assertNotIn("AddressSanitizer", result.stderr)
def test_default_selection_and_explicit_sdk_prerequisites(self):
probe = self.work / "guard.cpp"
probe.write_text('#include <helpers/RepeaterPsramStorage.h>\n'
'static_assert(MESH_REPEATER_PSRAM_TABLES == EXPECTED, "selection");\n',
encoding="ascii")
cases = [([], 0, True, SDK), (["ESP32_PLATFORM=1", "STATION_G3_ESP32=1"], 0, True, SDK),
(["ESP32_PLATFORM=1", "BOARD_HAS_PSRAM=1"], 0, True, SDK),
(DEFINES[:-1], 0, True, SDK),
(DEFINES, 1, True, SDK), (DEFINES + ["MESH_REPEATER_PSRAM_TABLES=0"], 0, True, ""),
(["MESH_REPEATER_PSRAM_TABLES=1"], 1, False, SDK),
(DEFINES + ["MESH_REPEATER_PSRAM_TABLES=2"], 2, False, SDK),
(DEFINES, 1, False, SDK.replace("CONFIG_IDF_TARGET_ESP32S3 1", "CONFIG_IDF_TARGET_ESP32 1")),
(DEFINES, 1, False, SDK.replace("CONFIG_SPIRAM_BOOT_INIT 1", "CONFIG_SPIRAM_BOOT_INIT 0")),
(DEFINES, 1, False, SDK.replace("CONFIG_SPIRAM_USE_MALLOC 1", "CONFIG_SPIRAM_USE_MALLOC 0"))]
for index, (defines, expected, success, config) in enumerate(cases):
with self.subTest(index=index, defines=defines):
config_dir = self.work / ("guard-%d" % index)
config_dir.mkdir()
(config_dir / "sdkconfig.h").write_text(config, encoding="ascii")
command = self.compiler + ["-std=c++17", "-fsyntax-only", "-I", str(config_dir),
"-I", str(self.work), "-I", str(ROOT / "src"), "-DEXPECTED=%d" % expected]
command += ["-D" + define for define in defines] + [str(probe)]
result = subprocess.run(command, capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode == 0, success, result.stdout + result.stderr)
if __name__ == "__main__":
unittest.main()
+327
View File
@@ -0,0 +1,327 @@
#!/usr/bin/env python3
"""Exercise the actual WS wrapper with SDK allocation/lifetime stubs."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
HEADERS = {
"esp_idf_version.h": "#pragma once\n",
"sdkconfig.h": "#pragma once\n#define CONFIG_WS_BUFFER_SIZE 1024\n",
"esp_transport.h": """
#pragma once
#include <stdint.h>
struct TestTransport;
typedef TestTransport* esp_transport_handle_t;
extern "C" void* esp_transport_get_context_data(esp_transport_handle_t);
extern "C" int esp_transport_destroy(esp_transport_handle_t);
""",
"esp_transport_ws.h": '#pragma once\n#include "esp_transport.h"\n',
"esp_heap_caps.h": """
#pragma once
#include <stddef.h>
#include <stdint.h>
#define MALLOC_CAP_SPIRAM 1U
#define MALLOC_CAP_8BIT 2U
#define MALLOC_CAP_INTERNAL 4U
#define MALLOC_CAP_DMA 8U
void* heap_caps_malloc(size_t, uint32_t);
size_t heap_caps_get_free_size(uint32_t);
size_t heap_caps_get_largest_free_block(uint32_t);
""",
}
HARNESS = r"""
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stdlib.h>
#include "esp_heap_caps.h"
static void* padded_malloc(size_t);
static void sdk_free(void*);
#define malloc padded_malloc
#define free sdk_free
#include "src/helpers/ESP32WsTransportFix.cpp"
#undef free
#undef malloc
struct TestTransport {
void* context;
};
#if ESP_IDF_VERSION_MAJOR == 4
using TestWs = transport_ws_t;
#else
struct TestWs {
char* path;
char* buffer;
char* sub_protocol;
char* user_agent;
char* headers;
esp_transport_handle_t parent;
};
#endif
enum Kind { SDK, GENERIC, EXTERNAL, INTERNAL };
struct Allocation {
void* ptr;
size_t size;
Kind kind;
};
static Allocation allocations[32];
static size_t allocation_count;
static unsigned destroy_calls;
static unsigned original_frees;
static unsigned generic_calls;
static unsigned external_calls;
static unsigned internal_calls;
static unsigned free_queries;
static unsigned largest_queries;
static bool fail_sdk;
static bool fail_external;
static bool fail_internal;
static bool fail_generic;
static size_t dma_free = 65536;
static size_t dma_largest = 8192;
static char* original;
static TestTransport parent;
static void* allocate(size_t bytes, Kind kind) {
assert(allocation_count < 32);
void* p = std::calloc(1, bytes);
assert(p);
allocations[allocation_count++] = {p, bytes, kind};
return p;
}
static Allocation lookup(void* ptr) {
for (size_t i = 0; i < allocation_count; ++i) {
if (allocations[i].ptr == ptr) return allocations[i];
}
std::abort();
}
static void sdk_free(void* ptr) {
if (!ptr) return;
for (size_t i = 0; i < allocation_count; ++i) {
if (allocations[i].ptr != ptr) continue;
if (ptr == original) ++original_frees;
allocations[i] = allocations[--allocation_count];
std::free(ptr);
return;
}
std::abort(); // Double free, wrong allocator, or parent destruction.
}
static void* padded_malloc(size_t bytes) {
++generic_calls;
assert(bytes == 1025);
return fail_generic ? nullptr : allocate(bytes, GENERIC);
}
void* heap_caps_malloc(size_t bytes, uint32_t caps) {
assert(bytes == 1025);
if (caps == (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)) {
++external_calls;
return fail_external ? nullptr : allocate(bytes, EXTERNAL);
}
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA | MALLOC_CAP_8BIT));
++internal_calls;
assert(dma_free >= 16384 + bytes && dma_largest >= bytes);
return fail_internal ? nullptr : allocate(bytes, INTERNAL);
}
size_t heap_caps_get_free_size(uint32_t caps) {
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
++free_queries;
return dma_free;
}
size_t heap_caps_get_largest_free_block(uint32_t caps) {
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
++largest_queries;
return dma_largest;
}
extern "C" void* esp_transport_get_context_data(esp_transport_handle_t t) {
assert(t && t != &parent);
return t->context;
}
extern "C" int esp_transport_destroy(esp_transport_handle_t t) {
assert(t && t != &parent);
++destroy_calls;
TestWs* ws = static_cast<TestWs*>(t->context);
assert(ws->parent == &parent);
sdk_free(ws->buffer);
sdk_free(ws->path);
sdk_free(ws->sub_protocol);
sdk_free(ws->user_agent);
sdk_free(ws->headers);
sdk_free(ws);
sdk_free(t);
return 0;
}
extern "C" esp_transport_handle_t __real_esp_transport_ws_init(esp_transport_handle_t p) {
assert(p == &parent);
if (fail_sdk) return nullptr;
auto* t = static_cast<TestTransport*>(allocate(sizeof(TestTransport), SDK));
auto* ws = static_cast<TestWs*>(allocate(sizeof(TestWs), SDK));
t->context = ws;
ws->parent = p;
ws->path = static_cast<char*>(allocate(2, SDK));
ws->sub_protocol = static_cast<char*>(allocate(5, SDK));
ws->user_agent = static_cast<char*>(allocate(8, SDK));
ws->headers = static_cast<char*>(allocate(8, SDK));
ws->buffer = static_cast<char*>(allocate(1024, SDK));
original = ws->buffer;
return t;
}
int main(int argc, char** argv) {
assert(argc == 2);
const std::string scenario(argv[1]);
bool expect_failure = false;
Kind expected = GENERIC;
if (scenario == "sdk_failure") {
fail_sdk = true;
expect_failure = true;
} else if (scenario == "external_success") {
// External success must not inspect or consume scarce internal memory.
dma_free = 0;
dma_largest = 0;
expected = EXTERNAL;
} else if (scenario == "internal_boundary") {
fail_external = true;
dma_free = 16384 + 1025;
dma_largest = 1025;
expected = INTERNAL;
} else if (scenario == "reserve_denied") {
fail_external = true;
dma_free = 16384 + 1024;
expect_failure = true;
} else if (scenario == "largest_denied") {
fail_external = true;
dma_largest = 1024;
expect_failure = true;
} else if (scenario == "both_fail") {
fail_external = true;
fail_internal = true;
expect_failure = true;
} else if (scenario == "generic_failure") {
fail_generic = true;
expect_failure = true;
} else {
assert(scenario == "generic_success" || scenario == "idf5_passthrough");
}
esp_transport_handle_t t = __wrap_esp_transport_ws_init(&parent);
if (expect_failure) {
assert(t == nullptr);
assert(allocation_count == 0);
assert(destroy_calls == (fail_sdk ? 0U : 1U));
assert(original_frees == (fail_sdk ? 0U : 1U));
} else {
assert(t && destroy_calls == 0);
auto* ws = static_cast<TestWs*>(t->context);
Allocation a = lookup(ws->buffer);
if (scenario == "idf5_passthrough") {
assert(ws->buffer == original && a.size == 1024 && a.kind == SDK);
assert(original_frees == 0);
} else {
assert(ws->buffer != original && a.size == 1025 && a.kind == expected);
assert(original_frees == 1);
// Exercise the exact off-by-one index the workaround must retain.
ws->buffer[1024] = '\0';
}
esp_transport_destroy(t);
assert(allocation_count == 0 && destroy_calls == 1 && original_frees == 1);
}
if (scenario == "external_success") {
assert(external_calls == 1 && internal_calls == 0 && generic_calls == 0);
assert(free_queries == 0 && largest_queries == 0);
} else if (scenario == "reserve_denied" || scenario == "largest_denied") {
assert(external_calls == 1 && internal_calls == 0 && generic_calls == 0);
assert(free_queries == 1 && largest_queries == 1);
} else if (scenario == "internal_boundary" || scenario == "both_fail") {
assert(external_calls == 1 && internal_calls == 1 && generic_calls == 0);
assert(free_queries == 1 && largest_queries == 1);
} else if (scenario == "generic_success" || scenario == "generic_failure") {
assert(external_calls == 0 && internal_calls == 0 && generic_calls == 1);
} else {
assert(external_calls == 0 && internal_calls == 0 && generic_calls == 0);
}
}
"""
class WsTransportPsramTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
if not compiler:
raise unittest.SkipTest("C++ compiler unavailable")
cls.temp = tempfile.TemporaryDirectory(prefix="ws-transport-psram-")
cls.addClassCleanup(cls.temp.cleanup)
work = Path(cls.temp.name)
for name, contents in HEADERS.items():
(work / name).write_text(contents, encoding="ascii")
source = work / "harness.cpp"
source.write_text(HARNESS, encoding="ascii")
cls.binaries = {}
variants = {
"g3_psram": (4, ["STATION_G3_ESP32", "BOARD_HAS_PSRAM"]),
"other_psram": (4, ["BOARD_HAS_PSRAM"]),
"g3_internal": (4, ["STATION_G3_ESP32"]),
"other_internal": (4, []),
"idf5": (5, ["STATION_G3_ESP32", "BOARD_HAS_PSRAM"]),
}
for name, (idf, macros) in variants.items():
binary = work / name
command = [
compiler, "-std=c++17", "-O1", "-g", "-Wall", "-Wextra", "-Werror",
"-Wno-unused-function", "-DESP_PLATFORM", f"-DESP_IDF_VERSION_MAJOR={idf}",
*[f"-D{macro}" for macro in macros], "-I", str(work), "-I", str(ROOT),
]
if os.name != "nt":
command += ["-fsanitize=address,undefined", "-fno-sanitize-recover=all",
"-fno-pie", "-no-pie"]
built = subprocess.run([*command, str(source), "-o", str(binary)],
capture_output=True, text=True)
if built.returncode:
raise RuntimeError(built.stdout + built.stderr)
cls.binaries[name] = binary
def run_scenario(self, variant, scenario):
result = subprocess.run([str(self.binaries[variant]), scenario],
capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_g3_psram_and_guarded_failure_lifecycles(self):
for scenario in ["external_success", "internal_boundary", "reserve_denied",
"largest_denied", "both_fail", "sdk_failure"]:
with self.subTest(scenario=scenario):
self.run_scenario("g3_psram", scenario)
def test_other_target_allocation_defaults_and_failure_cleanup(self):
for variant in ["other_psram", "g3_internal", "other_internal"]:
for scenario in ["generic_success", "generic_failure", "sdk_failure"]:
with self.subTest(variant=variant, scenario=scenario):
self.run_scenario(variant, scenario)
def test_idf5_is_unchanged_passthrough(self):
self.run_scenario("idf5", "idf5_passthrough")
self.run_scenario("idf5", "sdk_failure")
if __name__ == "__main__":
unittest.main()