feat(network): implement hostname configuration for automatic interfaces

This commit is contained in:
agessaman committed 2026-09-03 17:16:30 -07:00
1 parent 88b57db2c6
commit 0c65fc79db
8 files changed
+258 -2

No files matched your search

+1
View File
@@ -30,6 +30,7 @@ does not reflect the GoogleTest count — run the built binary directly
| `test_mqtt_topic_router` | `src/helpers/MQTTTopicRouter.h` | complete preset/custom topic-routing contract; MeshRank all types except raw; required identifiers; invalid inputs/slots; exact buffer boundaries |
| `test_mqtt_connection_policy` | `src/helpers/MQTTConnectionPolicy.h` | reconnect guard/backoff/stagger and breaker transitions; stable reset; JWT lifetime/renewal policy; exact timing boundaries and 32-bit `millis()` rollover; WiFi current-outage start sticky across STA reconnect attempts |
| `test_network_policy` | `src/helpers/NetworkPolicy.h` | transport-neutral MQTT link-transition actions and `start ota` selected-LAN versus forced/fallback SoftAP choice |
| `test_network_hostname` | `src/helpers/NetworkHostname.h` | DHCP-safe `meshcore-` hostname generation; invalid-character collapse; empty fallback; exact length boundary; stable identity suffix on truncation |
| `test_alert_fault_policy` | `src/helpers/AlertFaultPolicy.h` | WiFi/MQTT fault edge detector; `OutageSnapshot` (down / started_ms / initiating reason) fed to tick and `formatWifiAlert`; reason-8 reconnects change neither duration nor initiating reason; flap between status polls; down at `millis()==0`; packed 64-bit cross-task word; rate-limit floor and first-fire; 5 s poll cadence and `millis()` rollover |
| `test_display_viewport` | `src/helpers/ui/DisplayViewport.h`, `src/helpers/ui/DisplayFrameSignature.h` | logical-to-physical portrait mapping; fractional span coverage; fitted-width conversion; preferred/fallback text scaling; stable visible-frame change detection |
| `test_mqtt_packet_queue_policy` | `src/helpers/MQTTPacketQueuePolicy.h` | queue-full eviction; stale-disconnect flush; adaptive drain limits; bounded QoS0 retries; exact timing boundaries and 32-bit `millis()` rollover |
@@ -0,0 +1,95 @@
#include <gtest/gtest.h>
#include <cstring>
#include "helpers/NetworkHostname.h"
namespace {
const uint8_t kStableId[] = {0xab, 0xcd, 0x01};
void expectDhcpSafe(const char* hostname) {
ASSERT_NE(nullptr, hostname);
const size_t length = strlen(hostname);
EXPECT_GT(length, 0u);
EXPECT_LE(length, NetworkHostname::kMaxLength);
EXPECT_NE('-', hostname[0]);
EXPECT_NE('-', hostname[length - 1]);
for (size_t i = 0; i < length; ++i) {
const char ch = hostname[i];
EXPECT_TRUE((ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') || ch == '-')
<< "invalid byte at " << i;
}
}
} // namespace
TEST(NetworkHostname, PrefixesAndLowercasesReadableNodeName) {
char hostname[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(hostname, sizeof(hostname),
"Hill Repeater 2", kStableId,
sizeof(kStableId)));
EXPECT_STREQ("meshcore-hill-repeater-2", hostname);
expectDhcpSafe(hostname);
}
TEST(NetworkHostname, CollapsesInvalidRunsAndTrimsEdges) {
char hostname[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(hostname, sizeof(hostname),
" Adam's___Hill / Repeater! ",
kStableId, sizeof(kStableId)));
EXPECT_STREQ("meshcore-adam-s-hill-repeater", hostname);
expectDhcpSafe(hostname);
}
TEST(NetworkHostname, UsesDocumentedFallbackForEmptySanitizedName) {
char hostname[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(hostname, sizeof(hostname), "___ !!!",
kStableId, sizeof(kStableId)));
EXPECT_STREQ("meshcore-node", hostname);
expectDhcpSafe(hostname);
}
TEST(NetworkHostname, KeepsExactBoundaryWithoutIdentitySuffix) {
char hostname[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(hostname, sizeof(hostname),
"abcdefghijklmnopqrstuv", kStableId,
sizeof(kStableId)));
EXPECT_STREQ("meshcore-abcdefghijklmnopqrstuv", hostname);
EXPECT_EQ(NetworkHostname::kMaxLength, strlen(hostname));
}
TEST(NetworkHostname, TruncatesReadablePartAndAddsStableIdentitySuffix) {
char hostname[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(hostname, sizeof(hostname),
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
kStableId, sizeof(kStableId)));
EXPECT_STREQ("meshcore-abcdefghijklmno-abcd01", hostname);
EXPECT_EQ(NetworkHostname::kMaxLength, strlen(hostname));
expectDhcpSafe(hostname);
}
TEST(NetworkHostname, IdentitySuffixSeparatesOtherwiseCollidingNames) {
const uint8_t other_id[] = {0x12, 0x34, 0x56};
char first[NetworkHostname::kBufferSize];
char second[NetworkHostname::kBufferSize];
ASSERT_TRUE(NetworkHostname::build(first, sizeof(first),
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
kStableId, sizeof(kStableId)));
ASSERT_TRUE(NetworkHostname::build(second, sizeof(second),
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
other_id, sizeof(other_id)));
EXPECT_STRNE(first, second);
EXPECT_STREQ("meshcore-abcdefghijklmno-123456", second);
}
TEST(NetworkHostname, RejectsMissingOutputBuffer) {
EXPECT_FALSE(NetworkHostname::build(nullptr, 0, "node", kStableId,
sizeof(kStableId)));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}