Add configurable Companion Bluetooth addresses

This commit is contained in:
mikecarper committed 2026-09-07 18:29:00 -07:00
1 parent b25005f52f
commit 4b400ea56b
26 files changed
+2723 -1966

No files matched your search

+1 -1
View File
@@ -96,7 +96,7 @@ does not reflect the GoogleTest count -- run the built binary directly
| `test_lazy_persistence` | `src/helpers/LazyPersistence.h` | first-write scheduling without postponement, zero-sentinel rollover preservation, capped exponential save-failure backoff that mutations cannot defeat, and reset only after success |
| `test_client_acl_file_transaction` | `src/helpers/ClientACLFileTransaction.h` | verified temp publication, preservation of the prior ACL on verification/rename failure, post-commit cleanup-failure handling, and recovery at every temp/backup/primary boundary |
| `test_cad_timing` | `src/helpers/radiolib/CadTiming.h`, `LR2021SideDetectorConfig.h`, `RadioAirtime.h` | Cascade and slow-profile CAD deadlines; invalid airtime handling; bounded LR2021 side-detector parsing and LDRO recomputation |
| `test_companion_node_prefs` | `examples/companion_radio/NodePrefs.h` | independent device power saving, RXPS, Wi-Fi, and FEM preferences; one-time migration of the regressed power-saving default |
| `test_companion_node_prefs` | `examples/companion_radio/NodePrefs.h` | independent device power saving, RXPS, Wi-Fi, FEM, Bluetooth name, and Bluetooth address preferences; random-static address validation/generation; one-time migration of the regressed power-saving default |
| `test_config_serializer` | `src/helpers/ConfigSerializer.cpp`, Companion `NodePrefs` | escaped config save/load, whitespace and malformed input, unknown fields, and FEM/ESP-NOW bridge-format preference round trips |
| `test_deferred_cli_command` | `src/helpers/DeferredCliCommand.h` | copying authenticated command context, single-pending-command enforcement, clearing, and length rejection |
| `test_host_cli_bridge` | `src/helpers/HostCliBridge.h` | bounded request/reply parsing, Base64URL serial framing, correlation preservation, one-time service-claim proofs, request-token fields, line safety, and UTF-8-safe truncation |
+67
View File
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""Static contracts for Companion Bluetooth identity configuration."""
from pathlib import Path
import unittest
ROOT = Path(__file__).resolve().parents[1]
def source(relative: str) -> str:
return (ROOT / relative).read_text(encoding="utf-8")
class BluetoothMacContractTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.main = source("examples/companion_radio/main.cpp")
cls.mesh = source("examples/companion_radio/MyMesh.cpp")
cls.nrf_header = source("src/helpers/nrf52/SerialBLEInterface.h")
cls.nrf_source = source("src/helpers/nrf52/SerialBLEInterface.cpp")
cls.esp_header = source("src/helpers/esp32/SerialBLEInterface.h")
cls.esp_source = source("src/helpers/esp32/SerialBLEInterface.cpp")
def test_both_ble_backends_accept_the_same_configuration(self):
for header in (self.nrf_header, self.esp_header):
self.assertIn("const uint8_t* custom_address = nullptr", header)
self.assertIn("bool clear_bonds = false", header)
self.assertIn("the_mesh.getBLEPin(), bluetooth_address,", self.main)
self.assertIn("clear_bonds))", self.main)
def test_nrf52_uses_random_static_softdevice_address(self):
self.assertIn("BLE_GAP_ADDR_TYPE_RANDOM_STATIC", self.nrf_source)
self.assertIn("sd_ble_gap_addr_set(&address)", self.nrf_source)
self.assertIn(
"mesh::companion::BLUETOOTH_MAC_BYTES - 1 - i",
self.nrf_source,
)
def test_esp32_supports_nimble_and_bluedroid(self):
self.assertIn("BLEDevice::setOwnAddr(native_address)", self.esp_source)
self.assertIn("BLEDevice::setOwnAddrType(BLE_OWN_ADDR_RANDOM)", self.esp_source)
self.assertIn("setDeviceAddress(", self.esp_source)
self.assertIn("BLE_ADDR_TYPE_RANDOM", self.esp_source)
def test_every_boot_mode_is_stable_across_start_retries(self):
self.assertIn(
"companion_bluetooth_session_address_ready", self.main
)
self.assertIn(
"BLUETOOTH_MAC_RANDOM_EVERY_BOOT", self.main
)
self.assertIn("clear_bonds = true;", self.main)
def test_cli_exposes_custom_random_every_boot_and_default(self):
self.assertIn('"get bluetooth.mac"', self.mesh)
self.assertIn('"set bluetooth.mac"', self.mesh)
self.assertIn('strcmp(value, "random")', self.mesh)
self.assertIn('strcmp(value, "random-every-boot")', self.mesh)
self.assertIn('strcmp(value, "random everyboot")', self.mesh)
self.assertIn('strcmp(value, "default")', self.mesh)
self.assertIn("parseBluetoothMac(custom_value, address)", self.mesh)
if __name__ == "__main__":
unittest.main()
@@ -159,6 +159,84 @@ TEST(CompanionNodePrefs, BluetoothNameRequiresBoundedValidUtf8) {
EXPECT_FALSE(mesh::companion::isValidBluetoothName(malformed));
}
TEST(CompanionNodePrefs, BluetoothMacDefaultsToFactoryIdentity) {
CompanionNodePrefs prefs = {};
EXPECT_EQ(mesh::companion::BLUETOOTH_MAC_DEFAULT,
prefs.bluetooth_mac_mode);
EXPECT_FALSE(mesh::companion::hasCustomBluetoothMac(
prefs.bluetooth_mac));
}
TEST(CompanionNodePrefs, BluetoothMacParsesAndFormatsRandomStaticAddress) {
uint8_t address[mesh::companion::BLUETOOTH_MAC_BYTES] = {};
ASSERT_TRUE(mesh::companion::parseBluetoothMac(
"C2:11:22:33:44:55", address));
EXPECT_EQ(0xC2, address[0]);
EXPECT_EQ(0x55, address[5]);
char formatted[mesh::companion::BLUETOOTH_MAC_TEXT_SIZE];
ASSERT_TRUE(mesh::companion::formatBluetoothMac(
address, formatted, sizeof(formatted)));
EXPECT_STREQ("C2:11:22:33:44:55", formatted);
ASSERT_TRUE(mesh::companion::parseBluetoothMac(
"d6-aa-bb-cc-dd-ee", address));
ASSERT_TRUE(mesh::companion::formatBluetoothMac(
address, formatted, sizeof(formatted)));
EXPECT_STREQ("D6:AA:BB:CC:DD:EE", formatted);
}
TEST(CompanionNodePrefs, BluetoothMacRejectsInvalidOrMalformedAddress) {
uint8_t address[mesh::companion::BLUETOOTH_MAC_BYTES];
memset(address, 0xA5, sizeof(address));
EXPECT_FALSE(mesh::companion::parseBluetoothMac(
"02:11:22:33:44:55", address));
EXPECT_FALSE(mesh::companion::parseBluetoothMac(
"C0:00:00:00:00:00", address));
EXPECT_FALSE(mesh::companion::parseBluetoothMac(
"FF:FF:FF:FF:FF:FF", address));
EXPECT_FALSE(mesh::companion::parseBluetoothMac(
"C2:11-22:33:44:55", address));
EXPECT_FALSE(mesh::companion::parseBluetoothMac(
"C2:11:22:33:44", address));
for (size_t i = 0; i < sizeof(address); i++) {
EXPECT_EQ(0xA5, address[i]);
}
}
TEST(CompanionNodePrefs, BluetoothMacRandomBytesAreNormalized) {
uint8_t all_zero[mesh::companion::BLUETOOTH_MAC_BYTES] = {};
mesh::companion::makeRandomStaticBluetoothMac(all_zero);
EXPECT_TRUE(mesh::companion::isValidBluetoothMac(all_zero));
EXPECT_EQ(0xC0, all_zero[0]);
uint8_t all_one[mesh::companion::BLUETOOTH_MAC_BYTES];
memset(all_one, 0xFF, sizeof(all_one));
mesh::companion::makeRandomStaticBluetoothMac(all_one);
EXPECT_TRUE(mesh::companion::isValidBluetoothMac(all_one));
EXPECT_EQ(0xFE, all_one[5]);
}
TEST(CompanionNodePrefs, BluetoothMacModesDescribeSavedAndPerBootUse) {
EXPECT_TRUE(mesh::companion::isValidBluetoothMacMode(
mesh::companion::BLUETOOTH_MAC_DEFAULT));
EXPECT_TRUE(mesh::companion::isValidBluetoothMacMode(
mesh::companion::BLUETOOTH_MAC_CUSTOM));
EXPECT_TRUE(mesh::companion::isValidBluetoothMacMode(
mesh::companion::BLUETOOTH_MAC_RANDOM_SAVED));
EXPECT_TRUE(mesh::companion::isValidBluetoothMacMode(
mesh::companion::BLUETOOTH_MAC_RANDOM_EVERY_BOOT));
EXPECT_FALSE(mesh::companion::isValidBluetoothMacMode(4));
EXPECT_TRUE(mesh::companion::bluetoothMacModeUsesSavedAddress(
mesh::companion::BLUETOOTH_MAC_CUSTOM));
EXPECT_TRUE(mesh::companion::bluetoothMacModeUsesSavedAddress(
mesh::companion::BLUETOOTH_MAC_RANDOM_SAVED));
EXPECT_FALSE(mesh::companion::bluetoothMacModeUsesSavedAddress(
mesh::companion::BLUETOOTH_MAC_RANDOM_EVERY_BOOT));
}
TEST(CompanionNodePrefs, MigratesRegressedPowerSavingDefaultOnce) {
CompanionNodePrefs prefs = {};
prefs.powersaving_enabled = 0;
@@ -32,6 +32,7 @@ class CompanionSettingsPersistenceContractTests(unittest.TestCase):
cls.store = source("examples/companion_radio/DataStore.cpp")
cls.store_header = source("examples/companion_radio/DataStore.h")
cls.mesh = source("examples/companion_radio/MyMesh.cpp")
cls.prefs = source("examples/companion_radio/NodePrefs.h")
def test_preferences_load_has_an_observable_fail_closed_result(self):
self.assertIn("bool loadPrefs(CompanionNodePrefs& prefs", self.store_header)
@@ -110,6 +111,37 @@ class CompanionSettingsPersistenceContractTests(unittest.TestCase):
r"if\s*\(prefs_ready\s*&&\s*\(",
)
def test_bluetooth_mac_config_is_an_append_only_preference_tail(self):
cad_at = self.prefs.index("uint16_t cad_max_duration_ms")
mode_at = self.prefs.index("uint8_t bluetooth_mac_mode", cad_at)
address_at = self.prefs.index("uint8_t bluetooth_mac[", mode_at)
self.assertLess(cad_at, mode_at)
self.assertLess(mode_at, address_at)
load = function_body(
self.store,
"bool DataStore::loadPrefsInt(const char *filename",
)
self.assertIn("sizeof(loaded_prefs.bluetooth_mac_mode)", load)
self.assertIn("sizeof(loaded_prefs.bluetooth_mac)", load)
read_mode_at = load.index(
"readOptionalField(&loaded_prefs.bluetooth_mac_mode"
)
read_address_at = load.index(
"readOptionalField(loaded_prefs.bluetooth_mac", read_mode_at
)
self.assertLess(read_mode_at, read_address_at)
save = function_body(
self.store,
"bool DataStore::savePrefs(const CompanionNodePrefs& _prefs",
)
write_mode_at = save.index("&_prefs.bluetooth_mac_mode")
write_address_at = save.index(
"(uint8_t *)_prefs.bluetooth_mac,", write_mode_at
)
self.assertLess(write_mode_at, write_address_at)
def test_channel_source_discovery_and_open_failures_latch_quarantine(self):
load = function_body(self.store, "void DataStore::loadChannels(")
presence_at = load.index(
@@ -9,6 +9,7 @@
TEST(WebConfigKeys, AllowsKnownScalarKeys) {
EXPECT_TRUE(wcIsAllowedSetKey("name"));
EXPECT_TRUE(wcIsAllowedSetKey("bluetooth.name"));
EXPECT_TRUE(wcIsAllowedSetKey("bluetooth.mac"));
EXPECT_TRUE(wcIsAllowedSetKey("radio"));
EXPECT_TRUE(wcIsAllowedSetKey("radio.rxps"));
EXPECT_TRUE(wcIsAllowedSetKey("powersaving"));
@@ -68,8 +69,9 @@ TEST(WebConfigKeys, IsCaseSensitive) {
EXPECT_FALSE(wcIsAllowedSetKey("MQTT1.preset"));
}
TEST(WebConfigKeys, EspNowChannelAlwaysRequiresAReboot) {
TEST(WebConfigKeys, AddressChangingKeysAlwaysRequireAReboot) {
EXPECT_TRUE(wcSetKeyRequiresReboot("espnow.channel"));
EXPECT_TRUE(wcSetKeyRequiresReboot("bluetooth.mac"));
EXPECT_FALSE(wcSetKeyRequiresReboot(NULL));
EXPECT_FALSE(wcSetKeyRequiresReboot("espnow.channel.extra"));
EXPECT_FALSE(wcSetKeyRequiresReboot("ESPNOW.channel"));
@@ -77,6 +79,13 @@ TEST(WebConfigKeys, EspNowChannelAlwaysRequiresAReboot) {
EXPECT_FALSE(wcSetKeyRequiresReboot(""));
}
TEST(WebConfigKeys, EspNowChannelClassificationIsNarrow) {
EXPECT_TRUE(wcIsEspNowChannelKey("espnow.channel"));
EXPECT_FALSE(wcIsEspNowChannelKey("bluetooth.mac"));
EXPECT_FALSE(wcIsEspNowChannelKey("espnow.channel.extra"));
EXPECT_FALSE(wcIsEspNowChannelKey(NULL));
}
// ---- short-key OOB guard --------------------------------------------------
// The slot-prefix probe indexes key[4..6]; these short strings must be rejected
// without ever reading past the terminator.
+13
View File
@@ -188,6 +188,15 @@ class WebConfigUiTest(unittest.TestCase):
'pwdChanged?(d["wifi.pwd"]?"******":"(open network)")', review
)
def test_bluetooth_address_field_is_capability_gated_and_bound(self):
page = SOURCE.read_text(encoding="utf-8")
self.assertIn('data-cap="65536"><label>Bluetooth address</label>', page)
self.assertIn('data-k="bluetooth.mac"', page)
self.assertIn('case"bluetooth.mac":return r.bluetooth_mac||"";', page)
self.assertIn("random-every-boot", page)
self.assertIn('("bluetooth.mac" in setmap)', page)
self.assertIn("Bluetooth address saved [OK] - rebooting", page)
def test_scan_panel_exposes_automatic_updates_accessibly(self):
page = SOURCE.read_text(encoding="utf-8")
self.assertIn('id="wz-scan-btn"', page)
@@ -221,6 +230,10 @@ class WebConfigUiTest(unittest.TestCase):
self.assertIn("function restoreConfigLoadEdits(capture)", page)
self.assertIn("function pollScan(url,epoch)", page)
self.assertIn("function clearInheritedWiFiPassword(target,nextSsid)", page)
self.assertIn('data-cap="65536"><label>Bluetooth address</label>', page)
self.assertIn('data-k="bluetooth.mac"', page)
self.assertIn('case"bluetooth.mac":return r.bluetooth_mac||"";', page)
self.assertIn("random-every-boot", page)
def test_skipped_mqtt_does_not_pin_display_in_setup_mode(self):
main = (ROOT / "examples" / "companion_radio" / "main.cpp").read_text(