tests: cover CLI prefix stripping, set-pin gate, paged reply bounds, and ACL write failures

Changelog: -
This commit is contained in:
tth committed 2026-12-02 00:36:05 +01:00
1 parent 0dcc9c6e06
commit 5f31fc8e50
6 files changed
+192 -35

No files matched your search

+15 -5
View File
@@ -18,11 +18,12 @@ class File {
std::string _name;
size_t _pos = 0;
bool _write = false;
size_t _short_write = 0;
public:
File() = default;
File(std::map<std::string, std::string>* files, const std::string& name, bool write)
: _files(files), _name(name), _write(write) {
File(std::map<std::string, std::string>* files, const std::string& name, bool write, size_t short_write = 0)
: _files(files), _name(name), _write(write), _short_write(short_write) {
if (write) (*_files)[name] = "";
}
explicit operator bool() const { return _files != nullptr; }
@@ -34,8 +35,9 @@ public:
return (int)n;
}
size_t write(const uint8_t* buf, size_t len) {
(*_files)[_name].append((const char*)buf, len);
return len;
size_t n = (_short_write > 0 && _short_write < len) ? _short_write : len;
(*_files)[_name].append((const char*)buf, n);
return n;
}
void close() {}
};
@@ -46,14 +48,22 @@ class Adafruit_LittleFS {
public:
std::map<std::string, std::string> files;
int writes = 0;
bool fail_next_write_open = false;
size_t short_next_write = 0;
bool exists(const char* name) { return files.count(name) > 0; }
bool remove(const char* name) { return files.erase(name) > 0; }
bool mkdir(const char*) { return true; }
Adafruit_LittleFS_Namespace::File open(const char* name, uint8_t mode = FILE_O_READ) {
if (mode == FILE_O_WRITE) {
if (fail_next_write_open) {
fail_next_write_open = false;
return Adafruit_LittleFS_Namespace::File();
}
writes++;
return Adafruit_LittleFS_Namespace::File(&files, name, true);
size_t sw = short_next_write;
short_next_write = 0;
return Adafruit_LittleFS_Namespace::File(&files, name, true, sw);
}
if (!exists(name)) return Adafruit_LittleFS_Namespace::File();
return Adafruit_LittleFS_Namespace::File(&files, name, false);
@@ -187,6 +187,44 @@ TEST(AclReplayFloor, AlternatesSlotsAcrossReboots) {
}
}
TEST(AclReplayFloor, FailedOpenDoesNotRotateSlot) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW - 30)); // slot 0 -> next slot 1
ASSERT_TRUE(n.accept(c, NOW - 20)); // slot 1 -> next slot 0 (oldest)
n.acl->save(&n.fs);
// Open fails on slot 0: must NOT advance to slot 1 (which holds NOW - 20).
n.fs.fail_next_write_open = true;
ASSERT_TRUE(n.accept(c, NOW - 15));
// Next write is torn; because slot stayed 0, slot 1 (NOW - 20) stays intact.
n.fs.short_next_write = 5;
ASSERT_TRUE(n.accept(c, NOW - 10));
n.boot();
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
EXPECT_EQ(c->last_timestamp, NOW - 20);
}
TEST(AclReplayFloor, ShortWriteDoesNotRotateSlot) {
Node n;
ClientInfo* c = n.admin(1);
ASSERT_TRUE(n.accept(c, NOW - 30)); // slot 0
ASSERT_TRUE(n.accept(c, NOW - 20)); // slot 1
n.acl->save(&n.fs);
// Two consecutive short writes must both target slot 0 and leave slot 1 intact.
n.fs.short_next_write = 4;
ASSERT_TRUE(n.accept(c, NOW - 15));
n.fs.short_next_write = 4;
ASSERT_TRUE(n.accept(c, NOW - 10));
n.boot();
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
EXPECT_EQ(c->last_timestamp, NOW - 20);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
+82
View File
@@ -49,6 +49,88 @@ TEST(CliArgsFloat, RefusesNanInfExponentAndGarbage) {
EXPECT_FALSE(parseFloat("91", -90, 90, &f));
EXPECT_EQ(f, 1);
}
TEST(CliPrefix, StripsSpacesAndReflectsPrefixWithBudget) {
char buf[32] = {};
char* reply = buf;
int max_len = 150;
const char* cmd = " 01| set pin 123456";
cli_args::stripPrefix(cmd, reply, &max_len);
EXPECT_STREQ(cmd, "set pin 123456");
EXPECT_STREQ(buf, "01|");
EXPECT_EQ(reply, buf + 3);
EXPECT_EQ(max_len, 147);
}
TEST(CliPrefix, LeadingSpacesWithoutPrefix) {
char buf[16] = {};
char* reply = buf;
int max_len = 150;
const char* cmd = " set pin 123456";
cli_args::stripPrefix(cmd, reply, &max_len);
EXPECT_STREQ(cmd, "set pin 123456");
EXPECT_STREQ(buf, "");
EXPECT_EQ(reply, buf);
EXPECT_EQ(max_len, 150);
}
TEST(CliPrefix, TinyBudgetDoesNotOverflowOrGoNegative) {
char buf[4] = {'X', 'X', 'X', 0};
char* reply = buf;
int max_len = 2;
const char* cmd = "01|nodes";
cli_args::stripPrefix(cmd, reply, &max_len);
EXPECT_STREQ(cmd, "nodes");
EXPECT_EQ(reply, buf);
EXPECT_EQ(max_len, 2);
}
TEST(CliSetPinGate, EnforcesTransportAndLockedBuild) {
const char* err = nullptr;
// Local USB (unsecured, not rescue) -> blocked
EXPECT_FALSE(cli_args::canSetPin(0, false, false, false, &err));
EXPECT_STREQ(err, "Error: set pin only over paired BLE");
// Locked CLI rescue -> blocked
EXPECT_FALSE(cli_args::canSetPin(0, true, true, false, &err));
EXPECT_STREQ(err, "Error: locked build");
// Paired BLE -> allowed
EXPECT_TRUE(cli_args::canSetPin(0, false, true, true, &err));
// Unlocked CLI rescue -> allowed
EXPECT_TRUE(cli_args::canSetPin(0, true, false, false, &err));
// Remote ACL admin (sender_timestamp != 0) -> allowed
EXPECT_TRUE(cli_args::canSetPin(1763400000u, false, true, false, &err));
}
TEST(CliPagedReply, NeverOverflowsSmallMeshBufferWithPrefix) {
// Exact mesh CLI size: 150 B reply buffer, 3 B taken by "01|", leaving 147 B.
char buf[150] = {};
char* reply = buf;
int max_len = (int)sizeof(buf);
const char* cmd = "01|nodes";
cli_args::stripPrefix(cmd, reply, &max_len);
char* dp = reply;
std::string long_line(140, 'A');
long_line.push_back('\n');
// 141 B line + 24 B trailer reserve > 147 B -> must refuse even as first line
EXPECT_FALSE(cli_args::appendLine(dp, reply, max_len, 24, long_line.c_str()));
EXPECT_EQ(dp, reply);
std::string short_line(50, 'B');
short_line.push_back('\n');
EXPECT_TRUE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
EXPECT_TRUE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
EXPECT_FALSE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
dp += sprintf(dp, "E,2,3");
*dp = 0;
EXPECT_LT((size_t)(dp - buf), sizeof(buf));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();