Share the Companion terminal with the WiFi console
This commit is contained in:
1 parent
cb79dcd4e3
commit
d178cf1474
20 files changed
+3047
-2062
No files matched your search
@@ -66,6 +66,16 @@ def esp_fixture(path, modern=False, fragmented=False):
|
||||
|
||||
|
||||
class FirmwareRamTest(unittest.TestCase):
|
||||
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")
|
||||
plain = ram.requirements("ESP32_PLATFORM", defines, "v4_companion")
|
||||
psram = ram.requirements("ESP32_PLATFORM", {**defines, "BOARD_HAS_PSRAM": 1}, "v4_companion")
|
||||
self.assertEqual(plain["required_heap_bytes"] - base["required_heap_bytes"], 6144)
|
||||
self.assertEqual(psram["required_heap_bytes"] - base["required_heap_bytes"], 2048)
|
||||
repeater = ram.requirements("ESP32_PLATFORM", defines, "v4_repeater")
|
||||
self.assertNotIn("browser_terminal_session", repeater["components"])
|
||||
|
||||
def test_longer_display_previews_reserve_heap_and_contiguous_history(self):
|
||||
defines = {"DISPLAY_CLASS": "SSD1306Display", "UI_SMALL_MESSAGE_FONT": 0}
|
||||
base = ram.requirements("ESP32_PLATFORM", defines, "v4_companion")
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <helpers/TerminalSession.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
TEST(TerminalSession, AcceptsFullContactCardsAndErasesCommandAfterExecution) {
|
||||
mesh::TerminalCommandQueue q;
|
||||
const std::string card = "import meshcore://" + std::string(510, 'a');
|
||||
ASSERT_EQ(mesh::TerminalCommandQueue::Accepted, q.submit(1, card.c_str(), card.size()));
|
||||
EXPECT_STREQ(card.c_str(), q.command);
|
||||
q.finish();
|
||||
EXPECT_FALSE(q.pending);
|
||||
for (char byte : q.command) EXPECT_EQ(0, byte);
|
||||
}
|
||||
|
||||
TEST(TerminalSession, RetriesNeverExecuteAgainIncludingAfterLaterCommands) {
|
||||
mesh::TerminalCommandQueue q;
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Accepted, q.submit(1, "send hello", 10));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Replay, q.submit(1, "send hello", 10));
|
||||
q.finish();
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Accepted, q.submit(2, "list", 4));
|
||||
q.finish();
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Replay, q.submit(1, "send hello", 10));
|
||||
EXPECT_FALSE(q.pending);
|
||||
}
|
||||
|
||||
TEST(TerminalSession, RejectsConcurrentAndOutOfOrderCommandsWithoutChangingQueue) {
|
||||
mesh::TerminalCommandQueue q;
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::OutOfOrder, q.submit(2, "list", 4));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Accepted, q.submit(1, "card", 4));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Busy, q.submit(2, "list", 4));
|
||||
EXPECT_STREQ("card", q.command);
|
||||
q.finish();
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::OutOfOrder, q.submit(3, "list", 4));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::OutOfOrder, q.submit(0, "list", 4));
|
||||
}
|
||||
|
||||
TEST(TerminalSession, ValidatesLengthAndRejectsEmbeddedCommands) {
|
||||
mesh::TerminalCommandQueue q;
|
||||
const std::string limit(mesh::kTerminalCommandCapacity - 1, 'x');
|
||||
const std::string oversized(mesh::kTerminalCommandCapacity, 'x');
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Invalid, q.submit(1, oversized.c_str(), oversized.size()));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Invalid, q.submit(1, "list\nreboot", 11));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Invalid, q.submit(1, "list\rreboot", 11));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Invalid, q.submit(1, "list\0reboot", 11));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Invalid, q.submit(1, "", 0));
|
||||
EXPECT_EQ(mesh::TerminalCommandQueue::Accepted, q.submit(1, limit.c_str(), limit.size()));
|
||||
}
|
||||
|
||||
TEST(TerminalSession, PagesAFull350ContactListAndAllowsReadRetries) {
|
||||
std::vector<char> storage(mesh::kTerminalOutputCapacity);
|
||||
mesh::TerminalOutputBuffer buffer(storage.data(), storage.size());
|
||||
std::string input;
|
||||
for (int i = 0; i < 350; ++i) input += " " + std::string(31, 'n') + " (Repeater) - 123 days ago\r\n";
|
||||
buffer.append(reinterpret_cast<const uint8_t*>(input.data()), input.size());
|
||||
std::string actual;
|
||||
uint64_t cursor = 0;
|
||||
bool lost;
|
||||
char page[1025], retry[1025];
|
||||
while (cursor < buffer.end()) {
|
||||
uint64_t old = cursor;
|
||||
const size_t n = buffer.read(cursor, page, sizeof(page), lost);
|
||||
ASSERT_FALSE(lost);
|
||||
ASSERT_GT(n, 0u);
|
||||
EXPECT_EQ(n, buffer.read(old, retry, sizeof(retry), lost));
|
||||
EXPECT_STREQ(page, retry);
|
||||
actual.append(page, n);
|
||||
}
|
||||
EXPECT_EQ(input, actual);
|
||||
}
|
||||
|
||||
TEST(TerminalSession, ReportsExpiredOutputAndNeverWritesOutsideCapacity) {
|
||||
char storage[6] = {'!', 0, 0, 0, 0, '!'};
|
||||
mesh::TerminalOutputBuffer buffer(storage + 1, 4);
|
||||
buffer.append(reinterpret_cast<const uint8_t*>("abcdef"), 6);
|
||||
char out[8];
|
||||
uint64_t cursor = 0;
|
||||
bool lost;
|
||||
EXPECT_EQ(4u, buffer.read(cursor, out, sizeof(out), lost));
|
||||
EXPECT_TRUE(lost);
|
||||
EXPECT_STREQ("cdef", out);
|
||||
EXPECT_EQ('!', storage[0]);
|
||||
EXPECT_EQ('!', storage[5]);
|
||||
EXPECT_EQ(0u, buffer.read(cursor, out, sizeof(out), lost));
|
||||
EXPECT_FALSE(lost);
|
||||
}
|
||||
|
||||
TEST(TerminalSession, KeepsUtf8IntactAcrossOutputPages) {
|
||||
char storage[32];
|
||||
mesh::TerminalOutputBuffer buffer(storage, sizeof(storage));
|
||||
const std::string input = "ab\xF0\x9F\x98\x80" "cd\xC3\xA9" "ef";
|
||||
buffer.append(reinterpret_cast<const uint8_t*>(input.data()), input.size());
|
||||
char page[6];
|
||||
uint64_t cursor = 0;
|
||||
bool lost;
|
||||
EXPECT_EQ(2u, buffer.read(cursor, page, sizeof(page), lost));
|
||||
EXPECT_STREQ("ab", page);
|
||||
std::string actual(page);
|
||||
while (cursor < buffer.end()) {
|
||||
const size_t n = buffer.read(cursor, page, sizeof(page), lost);
|
||||
ASSERT_GT(n, 0u);
|
||||
actual.append(page, n);
|
||||
}
|
||||
EXPECT_EQ(input, actual);
|
||||
}
|
||||
|
||||
TEST(TerminalSession, GrowingAndShrinkingPreservesUnreadTextAndAbsoluteCursors) {
|
||||
char small[4], large[12], reduced[4], out[16];
|
||||
mesh::TerminalOutputBuffer buffer(small, sizeof(small));
|
||||
buffer.append(reinterpret_cast<const uint8_t*>("abcdef"), 6);
|
||||
buffer.rebind(large, sizeof(large));
|
||||
buffer.append(reinterpret_cast<const uint8_t*>("ghijkl"), 6);
|
||||
uint64_t cursor = 0;
|
||||
bool lost;
|
||||
EXPECT_EQ(10u, buffer.read(cursor, out, sizeof(out), lost));
|
||||
EXPECT_TRUE(lost);
|
||||
EXPECT_STREQ("cdefghijkl", out);
|
||||
buffer.rebind(reduced, sizeof(reduced));
|
||||
cursor = 8;
|
||||
EXPECT_EQ(4u, buffer.read(cursor, out, sizeof(out), lost));
|
||||
EXPECT_FALSE(lost);
|
||||
EXPECT_STREQ("ijkl", out);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
"""Run the production terminal allocator/Stream against failing heap fixtures."""
|
||||
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class WebTerminalStreamTest(unittest.TestCase):
|
||||
def test_growth_drain_failure_and_release(self):
|
||||
headers = {
|
||||
"Arduino.h": """#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
class Print { public: virtual ~Print() {};
|
||||
virtual size_t write(uint8_t)=0;
|
||||
virtual size_t write(const uint8_t*,size_t)=0;
|
||||
};
|
||||
class Stream: public Print { public:
|
||||
virtual int available()=0; virtual int read()=0; virtual int peek()=0;
|
||||
virtual void flush()=0;
|
||||
};
|
||||
""",
|
||||
"esp_heap_caps.h": """#pragma once
|
||||
#define MALLOC_CAP_SPIRAM 1
|
||||
#define MALLOC_CAP_INTERNAL 2
|
||||
#define MALLOC_CAP_8BIT 4
|
||||
void* heap_caps_malloc(size_t,int);
|
||||
size_t heap_caps_get_free_size(int);
|
||||
""",
|
||||
"freertos/FreeRTOS.h": "#pragma once\n#define portMAX_DELAY 0\n",
|
||||
"freertos/semphr.h": """#pragma once
|
||||
using SemaphoreHandle_t=void*;
|
||||
inline void* xSemaphoreCreateMutex(){return reinterpret_cast<void*>(1);}
|
||||
inline void xSemaphoreTake(void*,int){}
|
||||
inline void xSemaphoreGive(void*){}
|
||||
inline void vSemaphoreDelete(void*){}
|
||||
""",
|
||||
}
|
||||
code = r"""
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <esp_heap_caps.h>
|
||||
bool psram = true;
|
||||
size_t internal_free = 0;
|
||||
std::map<void*,size_t> allocated;
|
||||
void* heap_caps_malloc(size_t n,int caps) {
|
||||
if (caps & MALLOC_CAP_SPIRAM) { if (!psram) return nullptr; }
|
||||
else if(n>internal_free) return nullptr;
|
||||
void* p=std::malloc(n); if(p) allocated[p]=n; return p;
|
||||
}
|
||||
size_t heap_caps_get_free_size(int){return internal_free;}
|
||||
void tracked_free(void* p){allocated.erase(p);std::free(p);}
|
||||
#define free tracked_free
|
||||
#include <helpers/esp32/WebTerminalStream.h>
|
||||
#undef free
|
||||
size_t bytes(){size_t n=0;for(auto& p:allocated)n+=p.second;return n;}
|
||||
int main(){
|
||||
{
|
||||
WebTerminalStream terminal;
|
||||
assert(terminal.ready() && bytes()==4096);
|
||||
std::string data(24000,'x');
|
||||
assert(terminal.write(reinterpret_cast<const uint8_t*>(data.data()),data.size())==data.size());
|
||||
assert(bytes()==32768);
|
||||
std::string result; uint64_t cursor=0; char chunk[1025]; bool lost;
|
||||
while(cursor<data.size()) {
|
||||
size_t n=terminal.readOutput(cursor,chunk,sizeof(chunk),lost);
|
||||
assert(n && !lost);result.append(chunk,n);
|
||||
}
|
||||
terminal.readOutput(cursor,chunk,sizeof(chunk),lost);
|
||||
assert(result==data && bytes()==4096);
|
||||
}
|
||||
assert(allocated.empty());
|
||||
psram=false;internal_free=8192;
|
||||
{WebTerminalStream t;assert(!t.ready());assert(t.write('x')==0);}
|
||||
assert(allocated.empty());
|
||||
internal_free=40960;
|
||||
{
|
||||
WebTerminalStream t;assert(t.ready() && bytes()==4096);
|
||||
std::string data(24000,'a');t.write(reinterpret_cast<const uint8_t*>(data.data()),data.size());
|
||||
assert(bytes()==4096); // refused growth preserves the live buffer
|
||||
uint64_t cursor=0;char chunk[1025];bool lost=false;
|
||||
assert(t.readOutput(cursor,chunk,sizeof(chunk),lost)>0 && lost);
|
||||
}
|
||||
assert(allocated.empty());
|
||||
}
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
folder = Path(temp)
|
||||
for name, text in headers.items():
|
||||
path = folder / name
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="ascii")
|
||||
binary = folder / "stream"
|
||||
subprocess.run([
|
||||
"c++", "-std=c++17", "-x", "c++", "-", "-I" + str(folder),
|
||||
"-I" + str(ROOT / "src"), "-fsanitize=address,undefined",
|
||||
"-fno-pie", "-no-pie", "-o", str(binary),
|
||||
], input=code, text=True, check=True, capture_output=True)
|
||||
subprocess.run([str(binary)], check=True, capture_output=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -191,6 +191,65 @@ class WebConfigUiRuntimeTest(unittest.TestCase):
|
||||
"set bridge.enabled ", "get mqtt1.password", "set mqtt2.preset "):
|
||||
self.assertNotIn(command, suggestions)
|
||||
|
||||
def test_stream_terminal_imports_long_cards_pages_lists_and_receives_late_replies(self):
|
||||
status, config = self.setup_values()
|
||||
status.update(mode="lan", cli=True, terminal_stream=True,
|
||||
terminal_max_command=541, password_supported=False)
|
||||
prelude = """
|
||||
<script>
|
||||
(function(){
|
||||
var status=%s,config=%s,commands=[],sequence=0,output="",session="";
|
||||
function response(value){return Promise.resolve({ok:true,status:200,json:function(){return Promise.resolve(value)}})}
|
||||
window.fetch=function(path,options){
|
||||
if(path==="/api/status")return response(status);
|
||||
if(path==="/api/config")return response(config);
|
||||
if(path==="/api/presets")return response({presets:[]});
|
||||
if(path==="/api/terminal"){
|
||||
var p=JSON.parse(options.body);commands.push(p.command);sequence=p.seq;session=p.session;
|
||||
if(p.command.indexOf("import ")===0){output+="OK - contact import queued\\n";return Promise.reject(new Error("lost POST response"))}
|
||||
if(p.command==="list")for(var i=0;i<350;i++)output+="contact-"+i+" (Repeater)\\n";
|
||||
if(p.command.indexOf("to ")===0)output+="Selected recipient test\\n";
|
||||
if(p.command==="help")output+="firmware help: import, list, to, send\\n";
|
||||
return response({accepted:true});
|
||||
}
|
||||
if(path.indexOf("/api/terminal?")===0){
|
||||
var q=new URLSearchParams(path.split("?")[1]),from=Number(q.get("after")),text=output.slice(from,from+1024);
|
||||
return response({output:text,cursor:from+text.length,seq:sequence,done:true,closed:false,lost:false,more:from+text.length<output.length});
|
||||
}
|
||||
if(path.indexOf("/api/cli")===0)throw new Error("wrong CLI parser");
|
||||
return response({});
|
||||
};
|
||||
window.addEventListener("load",function(){
|
||||
setTimeout(function(){
|
||||
document.querySelector('#tabs button[data-t="cli"]').click();
|
||||
cliStreamRun(["import meshcore://"+"ab".repeat(255),"list","to test"]);
|
||||
},75);
|
||||
setTimeout(function(){output+="Late RF reply and ACK\\n"},300);
|
||||
setTimeout(function(){cliHelp()},550);
|
||||
setTimeout(function(){
|
||||
document.body.setAttribute("data-test-commands",JSON.stringify(commands));
|
||||
document.body.setAttribute("data-test-stream-output",document.getElementById("term-out").textContent);
|
||||
document.body.setAttribute("data-test-stream-busy",String(cli.busy));
|
||||
document.body.setAttribute("data-test-stream-session",session);
|
||||
},1000);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
""" % (json.dumps(status), json.dumps(config))
|
||||
dom = self.run_page(prelude, virtual_time=1200)
|
||||
import html
|
||||
commands = json.loads(html.unescape(re.search(
|
||||
r'data-test-commands="([^"]*)"', dom).group(1)))
|
||||
self.assertEqual(commands, ["import meshcore://" + "ab" * 255,
|
||||
"list", "to test", "help"])
|
||||
output = html.unescape(re.search(
|
||||
r'data-test-stream-output="([^"]*)"', dom).group(1))
|
||||
self.assertIn("contact-0 (Repeater)", output)
|
||||
self.assertIn("contact-349 (Repeater)", output)
|
||||
self.assertEqual(output.count("Late RF reply and ACK"), 1)
|
||||
self.assertIn("firmware help: import, list, to, send", output)
|
||||
self.assertIn('data-test-stream-busy="false"', dom)
|
||||
|
||||
def test_console_stays_hidden_when_disabled_or_on_setup_ap(self):
|
||||
for mode in ("lan", "setup"):
|
||||
with self.subTest(mode=mode):
|
||||
|
||||
Reference in new issue
Block a user