feat(#774): fake radio remote nodes (login and CLI)
FakeRemoteNode plays a repeater, room server or sensor. CMD_SEND_LOGIN
answers RESP_CODE_SENT tagged with the key prefix, then the node's
new-format LOGIN_SUCCESS (admin or guest) on the fake clock; a wrong
password gets no answer, as simple_repeater handleLoginReq returns 0.
Only an admin session runs CLI (the repeater's isAdmin gate); replies
come back as TXT_TYPE_CLI_DATA messages. Commands and replies copied
from CommonCLI: get radio/tx/path.hash.mode (ftoa3 formatting), set
radio ("OK - reboot to apply"), tempradio (applies 2 s after the reply,
reverts after its minutes), set path.hash.mode 0-2, set tx, ver,
version, Unknown command.
Stock vs Offband, from source: Offband rejects a fifth set radio part
(#299), stock ignores it; stock has no `version` and answers it as
`ver`. 74 tests, driven by the client's own login and CLI builders.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat/757-fake-radio-behaviors
parent
fa509dcf90
commit
14b05454e4
@ -0,0 +1,183 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
|
||||
import '../support/fake_radio/fake_radio.dart';
|
||||
import '../support/fake_radio/fake_radio_codes.dart';
|
||||
import '../support/fake_radio/fake_radio_seed.dart';
|
||||
import '../support/fake_radio/fake_remote_node.dart';
|
||||
|
||||
// #774 (B3 of #755): repeaters, room servers and sensors over the mesh,
|
||||
// driven by the client's own login and CLI frames.
|
||||
|
||||
void main() {
|
||||
final repeater = FakeContact.keyed(0x51, 'Rpt', type: fwAdvTypeRepeater);
|
||||
|
||||
(FakeRadio, FakeRemoteNode, List<Uint8List>) setUpNode({
|
||||
bool offband = true,
|
||||
}) {
|
||||
final radio = FakeRadio();
|
||||
final node = radio.addRemoteNode(
|
||||
FakeRemoteNode(
|
||||
contact: repeater,
|
||||
guestPassword: 'guest',
|
||||
offband: offband,
|
||||
),
|
||||
);
|
||||
final pushes = <Uint8List>[];
|
||||
radio.pushes.listen(pushes.add);
|
||||
return (radio, node, pushes);
|
||||
}
|
||||
|
||||
void login(FakeRadio radio, String password) {
|
||||
radio.handle(buildSendLoginFrame(repeater.publicKey, password));
|
||||
radio.clock.advance(const Duration(seconds: 1));
|
||||
}
|
||||
|
||||
/// Sends one CLI command and returns the node's reply text, or null.
|
||||
String? cli(FakeRadio radio, String command) {
|
||||
radio.offlineQueue.clear();
|
||||
radio.handle(buildSendCliCommandFrame(repeater.publicKey, command));
|
||||
radio.clock.advance(const Duration(seconds: 1));
|
||||
if (radio.offlineQueue.isEmpty) return null;
|
||||
final f = radio.offlineQueue.single;
|
||||
expect(f[0], fwRespContactMsgRecvV3);
|
||||
expect(f.sublist(4, 10), repeater.publicKey.sublist(0, 6));
|
||||
expect(f[11], fwTxtTypeCliData);
|
||||
return utf8.decode(f.sublist(16));
|
||||
}
|
||||
|
||||
group('login', () {
|
||||
test('admin: SENT tagged with the key, then LOGIN_SUCCESS as admin', () {
|
||||
final (radio, _, pushes) = setUpNode();
|
||||
final sent = radio
|
||||
.handle(buildSendLoginFrame(repeater.publicKey, 'password'))
|
||||
.single;
|
||||
expect(sent[0], fwRespSent);
|
||||
expect(sent.sublist(2, 6), repeater.publicKey.sublist(0, 4));
|
||||
expect(pushes, isEmpty);
|
||||
radio.clock.advance(const Duration(seconds: 1));
|
||||
final ok = pushes.single;
|
||||
expect(ok[0], fwPushLoginSuccess);
|
||||
expect(ok[1], 1); // is_admin
|
||||
expect(ok.sublist(2, 8), repeater.publicKey.sublist(0, 6));
|
||||
expect(ok[12], 3); // PERM_ACL_ADMIN
|
||||
expect(ok.length, 14);
|
||||
});
|
||||
|
||||
test('guest: success, not admin, and CLI is ignored', () {
|
||||
final (radio, _, pushes) = setUpNode();
|
||||
login(radio, 'guest');
|
||||
expect(pushes.single[1], 0);
|
||||
expect(pushes.single[12], 0);
|
||||
expect(cli(radio, 'get radio'), isNull);
|
||||
});
|
||||
|
||||
test('a wrong password gets no answer at all', () {
|
||||
final (radio, _, pushes) = setUpNode();
|
||||
login(radio, 'nope');
|
||||
radio.clock.advance(const Duration(minutes: 1));
|
||||
expect(pushes, isEmpty);
|
||||
});
|
||||
|
||||
test('without logging in, CLI is ignored', () {
|
||||
final (radio, _, _) = setUpNode();
|
||||
expect(cli(radio, 'get radio'), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('CLI as admin', () {
|
||||
test('get radio, tx and path hash mode', () {
|
||||
final (radio, _, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'get radio'), '> 910.525,62.5,7,5');
|
||||
expect(cli(radio, 'get tx'), '> 20');
|
||||
expect(cli(radio, 'get path.hash.mode'), '> 0');
|
||||
});
|
||||
|
||||
test('set radio saves for after reboot; bad values are refused', () {
|
||||
final (radio, node, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'set radio 909.75,500,10,5'), 'OK - reboot to apply');
|
||||
expect(node.freqMhz, 909.75);
|
||||
expect(cli(radio, 'get radio'), '> 909.75,500,10,5');
|
||||
expect(
|
||||
cli(radio, 'set radio 909.75,600,10,5'),
|
||||
'Error, invalid radio params',
|
||||
);
|
||||
});
|
||||
|
||||
test('set radio with a fifth part: Offband refuses, stock ignores it', () {
|
||||
var (radio, _, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(
|
||||
cli(radio, 'set radio 909.75,500,10,5,9'),
|
||||
'Error, too many params (expected freq,bw,sf,cr)',
|
||||
);
|
||||
(radio, _, _) = setUpNode(offband: false);
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'set radio 909.75,500,10,5,9'), 'OK - reboot to apply');
|
||||
});
|
||||
|
||||
test('tempradio applies 2 s after the reply and reverts on time', () {
|
||||
final (radio, node, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(
|
||||
cli(radio, 'tempradio 909.750,500.0,10,5,60'),
|
||||
'OK - temp params for 60 mins',
|
||||
);
|
||||
expect(node.tempRadio, isNull);
|
||||
radio.clock.advance(const Duration(seconds: 2));
|
||||
expect(node.tempRadio?.freqMhz, 909.75);
|
||||
expect(node.freqMhz, 910.525); // saved settings untouched
|
||||
radio.clock.advance(const Duration(minutes: 59, seconds: 59));
|
||||
expect(node.tempRadio, isNotNull);
|
||||
radio.clock.advance(const Duration(seconds: 1));
|
||||
expect(node.tempRadio, isNull);
|
||||
});
|
||||
|
||||
test('tempradio needs a positive duration and valid params', () {
|
||||
final (radio, _, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(
|
||||
cli(radio, 'tempradio 909.75,500,10,5,0'),
|
||||
'Error, invalid params',
|
||||
);
|
||||
expect(
|
||||
cli(radio, 'tempradio 909.75,500,10,4,60'),
|
||||
'Error, invalid params',
|
||||
);
|
||||
});
|
||||
|
||||
test('set path.hash.mode 0-2 and set tx', () {
|
||||
final (radio, node, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'set path.hash.mode 1'), 'OK');
|
||||
expect(node.pathHashMode, 1);
|
||||
expect(cli(radio, 'set path.hash.mode 3'), 'Error, must be 0,1, or 2');
|
||||
expect(cli(radio, 'set tx 14'), 'OK');
|
||||
expect(cli(radio, 'get tx'), '> 14');
|
||||
});
|
||||
|
||||
test(
|
||||
'ver everywhere; version is Offband-only, stock answers it as ver',
|
||||
() {
|
||||
var (radio, _, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'ver'), 'v1.17.0 (Build: 1 Oct 2026)');
|
||||
expect(cli(radio, 'version'), startsWith('Upstream MeshCore: v1.17.0'));
|
||||
(radio, _, _) = setUpNode(offband: false);
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'version'), 'v1.17.0 (Build: 1 Oct 2026)');
|
||||
},
|
||||
);
|
||||
|
||||
test('anything else is an unknown command', () {
|
||||
final (radio, _, _) = setUpNode();
|
||||
login(radio, 'password');
|
||||
expect(cli(radio, 'frobnicate'), 'Unknown command');
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,193 @@
|
||||
import 'fake_radio_seed.dart';
|
||||
|
||||
/// A repeater, room server or sensor out on the mesh, reached through the
|
||||
/// companion with login and CLI commands (#774). Its CLI replies are copied
|
||||
/// from firmware `src/helpers/CommonCLI.cpp`; [offband] picks the Offband or
|
||||
/// stock (upstream `companion-v1.17.1`) behavior where they differ.
|
||||
class FakeRemoteNode {
|
||||
FakeRemoteNode({
|
||||
required this.contact,
|
||||
this.adminPassword = 'password',
|
||||
this.guestPassword = '',
|
||||
this.offband = true,
|
||||
this.freqMhz = 910.525,
|
||||
this.bwKhz = 62.5,
|
||||
this.sf = 7,
|
||||
this.cr = 5,
|
||||
this.txPowerDbm = 20,
|
||||
this.pathHashMode = 0,
|
||||
this.firmwareVersion = 'v1.17.0',
|
||||
this.buildDate = '1 Oct 2026',
|
||||
this.offbandVersion = 'offband-v1.5.0-beta7',
|
||||
this.replyDelay = const Duration(seconds: 1),
|
||||
});
|
||||
|
||||
final FakeContact contact;
|
||||
final String adminPassword;
|
||||
final String guestPassword;
|
||||
final bool offband;
|
||||
|
||||
// Saved prefs. `set radio` writes these; they apply after a reboot.
|
||||
double freqMhz;
|
||||
double bwKhz;
|
||||
int sf;
|
||||
int cr;
|
||||
int txPowerDbm;
|
||||
int pathHashMode;
|
||||
|
||||
final String firmwareVersion;
|
||||
final String buildDate;
|
||||
final String offbandVersion;
|
||||
|
||||
/// How long a login or CLI reply takes to come back, on the fake clock.
|
||||
Duration replyDelay;
|
||||
|
||||
/// The temporary radio from `tempradio`, while it lasts.
|
||||
FakeTempRadio? tempRadio;
|
||||
|
||||
/// Every CLI command received, in order.
|
||||
final List<String> commands = [];
|
||||
|
||||
/// `handleLoginReq` (examples/simple_repeater/MyMesh.cpp:93-120): admin
|
||||
/// password, guest password, or no reply at all.
|
||||
FakeLoginResult? login(String password) {
|
||||
if (password == adminPassword) return FakeLoginResult.admin;
|
||||
if (password == guestPassword) return FakeLoginResult.guest;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// One CLI command, its reply text. A `tempradio` leaves its settings in
|
||||
/// [takePendingTempRadio] for the radio to schedule.
|
||||
String command(String text) {
|
||||
commands.add(text);
|
||||
if (offband && _word(text, 'version')) {
|
||||
// CommonCLI.cpp:650-657 (Offband identity, FF3 / #180).
|
||||
return 'Upstream MeshCore: $firmwareVersion ($buildDate)\n'
|
||||
'Offband fork: $offbandVersion (sha 0000000, main, built $buildDate)';
|
||||
}
|
||||
if (text.startsWith('ver')) {
|
||||
// CommonCLI.cpp:750-751. Stock has no `version`, so it lands here too.
|
||||
return '$firmwareVersion (Build: $buildDate)';
|
||||
}
|
||||
if (text.startsWith('tempradio ')) return _tempRadio(text.substring(10));
|
||||
if (text.startsWith('get ')) return _get(text.substring(4));
|
||||
if (text.startsWith('set ')) return _set(text.substring(4));
|
||||
return 'Unknown command';
|
||||
}
|
||||
|
||||
/// `tempradio f,bw,sf,cr,mins` (CommonCLI.cpp:719-733).
|
||||
String _tempRadio(String args) {
|
||||
final p = args.split(',');
|
||||
final f = p.isNotEmpty ? _float(p[0]) : 0.0;
|
||||
final bw = p.length > 1 ? _float(p[1]) : 0.0;
|
||||
final s = p.length > 2 ? _int(p[2]) : 0;
|
||||
final c = p.length > 3 ? _int(p[3]) : 0;
|
||||
final mins = p.length > 4 ? _int(p[4]) : 0;
|
||||
if (!_validRadio(f, bw, s, c) || mins <= 0) return 'Error, invalid params';
|
||||
_pendingTemp = FakeTempRadio(f, bw, s, c, mins);
|
||||
return 'OK - temp params for $mins mins';
|
||||
}
|
||||
|
||||
FakeTempRadio? _pendingTemp;
|
||||
|
||||
/// The `tempradio` the last command asked for, if any (taken once).
|
||||
FakeTempRadio? takePendingTempRadio() {
|
||||
final t = _pendingTemp;
|
||||
_pendingTemp = null;
|
||||
return t;
|
||||
}
|
||||
|
||||
/// `handleGetCmd` (CommonCLI.cpp:1653-1697).
|
||||
String _get(String key) => switch (key.trim()) {
|
||||
'radio' => '> ${_ftoa3(freqMhz)},${_ftoa3(bwKhz)},$sf,$cr',
|
||||
'tx' => '> $txPowerDbm',
|
||||
'path.hash.mode' => '> $pathHashMode',
|
||||
'freq' => '> ${_ftoa3(freqMhz)}',
|
||||
_ => 'Unknown command',
|
||||
};
|
||||
|
||||
/// `handleSetCmd` (CommonCLI.cpp:1333-1482).
|
||||
String _set(String config) {
|
||||
if (config.startsWith('radio ')) {
|
||||
final p = config.substring(6).split(',');
|
||||
// Offband (#299) rejects a fifth part; stock (parseTextParts with 4)
|
||||
// ignores anything past the fourth.
|
||||
if (offband && p.length > 4) {
|
||||
return 'Error, too many params (expected freq,bw,sf,cr)';
|
||||
}
|
||||
final f = p.isNotEmpty ? _float(p[0]) : 0.0;
|
||||
final bw = p.length > 1 ? _float(p[1]) : 0.0;
|
||||
final s = p.length > 2 ? _int(p[2]) : 0;
|
||||
final c = p.length > 3 ? _int(p[3]) : 0;
|
||||
if (!_validRadio(f, bw, s, c)) return 'Error, invalid radio params';
|
||||
freqMhz = f;
|
||||
bwKhz = bw;
|
||||
sf = s;
|
||||
cr = c;
|
||||
return 'OK - reboot to apply';
|
||||
}
|
||||
if (config.startsWith('path.hash.mode ')) {
|
||||
final mode = _int(config.substring(15));
|
||||
if (mode >= 3 || mode < 0) return 'Error, must be 0,1, or 2';
|
||||
pathHashMode = mode;
|
||||
return 'OK';
|
||||
}
|
||||
if (config.startsWith('tx ')) {
|
||||
txPowerDbm = _int(config.substring(3));
|
||||
return 'OK';
|
||||
}
|
||||
return 'Unknown command';
|
||||
}
|
||||
|
||||
static bool _validRadio(double f, double bw, int s, int c) =>
|
||||
f >= 150.0 &&
|
||||
f <= 2500.0 &&
|
||||
s >= 5 &&
|
||||
s <= 12 &&
|
||||
c >= 5 &&
|
||||
c <= 8 &&
|
||||
bw >= 7.0 &&
|
||||
bw <= 500.0;
|
||||
|
||||
static bool _word(String text, String w) =>
|
||||
text == w || text.startsWith('$w ');
|
||||
|
||||
/// `strtof`: leading number, else 0.
|
||||
static double _float(String s) =>
|
||||
double.tryParse(
|
||||
RegExp(r'^\s*[-+]?\d*\.?\d+').firstMatch(s)?.group(0)?.trim() ?? '',
|
||||
) ??
|
||||
0.0;
|
||||
|
||||
/// `atoi`: leading integer, else 0.
|
||||
static int _int(String s) =>
|
||||
int.tryParse(
|
||||
RegExp(r'^\s*[-+]?\d+').firstMatch(s)?.group(0)?.trim() ?? '',
|
||||
) ??
|
||||
0;
|
||||
|
||||
/// `StrHelper::ftoa3` (src/helpers/TxtDataHelpers.cpp:143-154): three
|
||||
/// decimals, rounded, trailing zeros and a bare point dropped.
|
||||
static String _ftoa3(double f) {
|
||||
final v = (f * 1000 + (f >= 0 ? 0.5 : -0.5)).truncate();
|
||||
var s = '${v ~/ 1000}.${(v % 1000).abs().toString().padLeft(3, '0')}';
|
||||
while (s.endsWith('0')) {
|
||||
s = s.substring(0, s.length - 1);
|
||||
}
|
||||
if (s.endsWith('.')) s = s.substring(0, s.length - 1);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
enum FakeLoginResult { admin, guest }
|
||||
|
||||
/// A temporary radio setting from `tempradio`, reverting after [minutes].
|
||||
class FakeTempRadio {
|
||||
FakeTempRadio(this.freqMhz, this.bwKhz, this.sf, this.cr, this.minutes);
|
||||
|
||||
final double freqMhz;
|
||||
final double bwKhz;
|
||||
final int sf;
|
||||
final int cr;
|
||||
final int minutes;
|
||||
}
|
||||
Loading…
Reference in new issue