Merge branch 'dev' into ez_disable_discoveredContact_eviction_PR

This commit is contained in:
zjs81 authored and GitHub committed 2026-10-07 20:59:37 -07:00
commit 956c779e2c
195 files changed
+26503 -6523

No files matched your search

@@ -0,0 +1,29 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/models/channel_message.dart';
void main() {
test('outgoing reply keeps the selected target, not the latest post', () {
final older = ChannelMessage.outgoing('first post', 'Alice', 0);
ChannelMessage.outgoing('second post', 'Alice', 0);
final reply = ChannelMessage.outgoing(
'@[Alice] answer',
'Me',
0,
replyTo: older,
);
expect(reply.replyToMessageId, older.messageId);
expect(reply.replyToSenderName, 'Alice');
expect(reply.replyToText, 'first post');
expect(
ChannelMessage.parseReplyMention(reply.text)?.actualMessage,
'answer',
);
});
test('outgoing message without reply has no reply target', () {
final message = ChannelMessage.outgoing('hello', 'Me', 0);
expect(message.replyToMessageId, isNull);
});
}
+203
View File
@@ -0,0 +1,203 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/helpers/path_helper.dart';
import 'package:meshcore_open/models/channel_message.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/storage/contact_discovery_store.dart';
import 'package:meshcore_open/storage/contact_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
Uint8List _contactFrame(int pathLen, List<int> path) {
final writer = BytesBuilder();
writer.addByte(respCodeContact);
writer.add(List.generate(32, (i) => i + 1));
writer.addByte(advTypeRepeater);
writer.addByte(0);
writer.addByte(pathLen);
writer.add(Uint8List(64)..setRange(0, path.length, path));
writer.add(Uint8List(32)..setRange(0, 4, 'Node'.codeUnits));
writer.add([0x01, 0x00, 0x00, 0x00]);
writer.add(Uint8List(12));
return writer.toBytes();
}
Contact _contact({required int pathHashWidth}) => Contact(
publicKey: Uint8List.fromList(List.generate(32, (i) => i + 1)),
name: 'Node',
type: advTypeRepeater,
pathLength: 2,
path: Uint8List.fromList([0xA1, 0xA2, 0xB1, 0xB2]),
pathHashWidth: pathHashWidth,
lastSeen: DateTime.fromMillisecondsSinceEpoch(1000),
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
});
group('Contact.fromFrame keeps the path hash width', () {
test('2-byte hops', () {
final contact = Contact.fromFrame(
_contactFrame((1 << 6) | 2, [0xA1, 0xA2, 0xB1, 0xB2]),
)!;
expect(contact.pathLength, 2);
expect(contact.pathHashWidth, 2);
expect(contact.path, [0xA1, 0xA2, 0xB1, 0xB2]);
expect(PathHelper.splitPathBytes(contact.path, contact.pathHashWidth), [
[0xA1, 0xA2],
[0xB1, 0xB2],
]);
});
test('3-byte hops', () {
final contact = Contact.fromFrame(
_contactFrame((2 << 6) | 1, [0xA1, 0xA2, 0xA3]),
)!;
expect(contact.pathLength, 1);
expect(contact.pathHashWidth, 3);
expect(contact.path, [0xA1, 0xA2, 0xA3]);
});
test('flood defaults to width 1', () {
final contact = Contact.fromFrame(_contactFrame(0xFF, const []))!;
expect(contact.pathLength, -1);
expect(contact.pathHashWidth, 1);
});
test('copyWith preserves width', () {
final contact = _contact(pathHashWidth: 2).copyWith(name: 'Renamed');
expect(contact.pathHashWidth, 2);
});
});
group('Contact.inferPathHashWidth', () {
test('divides bytes by hops', () {
expect(Contact.inferPathHashWidth(2, 4), 2);
expect(Contact.inferPathHashWidth(1, 3), 3);
});
test('falls back to 1', () {
expect(Contact.inferPathHashWidth(0, 0), 1);
expect(Contact.inferPathHashWidth(-1, 0), 1);
expect(Contact.inferPathHashWidth(2, 3), 1);
expect(Contact.inferPathHashWidth(1, 4), 1);
});
});
group('Contact stores persist the path hash width', () {
test('ContactStore round-trip', () async {
final store = ContactStore()..publicKeyHex = '1234567890';
await store.saveContacts([_contact(pathHashWidth: 2)]);
final loaded = await store.loadContacts();
expect(loaded.single.pathHashWidth, 2);
expect(loaded.single.path, [0xA1, 0xA2, 0xB1, 0xB2]);
});
test('ContactDiscoveryStore round-trip', () async {
final store = ContactDiscoveryStore()..setPublicKeyHex = 'AABBCCDDEEFF00';
await store.saveContacts([_contact(pathHashWidth: 2)]);
final loaded = await store.loadContacts();
expect(loaded.single.pathHashWidth, 2);
});
test('legacy data without width is inferred from hops', () async {
final store = ContactStore()..publicKeyHex = '1234567890';
final json = {
'publicKey': base64Encode(List.generate(32, (i) => i + 1)),
'name': 'Legacy',
'type': advTypeRepeater,
'pathLength': 2,
'path': base64Encode([0xA1, 0xA2, 0xB1, 0xB2]),
'lastSeen': 1000,
};
await PrefsManager.instance.setString(store.keyFor, jsonEncode([json]));
final loaded = await store.loadContacts();
expect(loaded.single.pathHashWidth, 2);
});
test('legacy flood data without width falls back to 1', () async {
final store = ContactStore()..publicKeyHex = '1234567890';
final json = {
'publicKey': base64Encode(List.generate(32, (i) => i + 1)),
'name': 'Legacy',
'type': advTypeRepeater,
'pathLength': -1,
'path': base64Encode(const <int>[]),
'lastSeen': 1000,
};
await PrefsManager.instance.setString(store.keyFor, jsonEncode([json]));
final loaded = await store.loadContacts();
expect(loaded.single.pathHashWidth, 1);
});
test('legacy mode-encoded pathLength yields its width', () async {
final store = ContactStore()..publicKeyHex = '1234567890';
final json = {
'publicKey': base64Encode(List.generate(32, (i) => i + 1)),
'name': 'Legacy',
'type': advTypeRepeater,
'pathLength': (2 << 6) | 1,
'path': base64Encode([0xA1, 0xA2, 0xA3]),
'lastSeen': 1000,
};
await PrefsManager.instance.setString(store.keyFor, jsonEncode([json]));
final loaded = await store.loadContacts();
expect(loaded.single.pathLength, 1);
expect(loaded.single.pathHashWidth, 3);
});
});
group('ChannelMessage.fromFrame path_len 0xFF (direct)', () {
Uint8List frame({required bool v3}) {
final writer = BytesBuilder();
if (v3) {
writer.add([respCodeChannelMsgRecvV3, 0x10, 0x01, 0x00]);
} else {
writer.addByte(respCodeChannelMsgRecv);
}
writer.addByte(3); // channel index
writer.addByte(0xFF);
writer.addByte(txtTypePlain);
writer.add([0x01, 0x00, 0x00, 0x00]);
writer.add(utf8.encode('Alice: hi'));
writer.addByte(0);
return writer.toBytes();
}
test('V3 does not read 63 x 4-byte hops', () {
final message = ChannelMessage.fromFrame(frame(v3: true))!;
expect(message.text, 'hi');
expect(message.senderName, 'Alice');
expect(message.pathLength, isNull);
expect(message.pathBytes, isEmpty);
expect(message.pathHashWidth, isNull);
expect(message.channelIndex, 3);
});
test('non-V3 reads path_len unsigned', () {
final message = ChannelMessage.fromFrame(frame(v3: false))!;
expect(message.text, 'hi');
expect(message.pathLength, isNull);
});
});
test('PathHelper treats width 4 as invalid and clamps to 3', () {
expect(PathHelper.splitPathBytes([1, 2, 3, 4], 4), [
[1, 2, 3],
[4],
]);
});
test('buildSetPathHashModeFrame rejects reserved mode 3', () {
expect(buildSetPathHashModeFrame(3), [cmdSetPathHashMode, 0, 2]);
});
}
+24 -1
View File
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart' as crypto;
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/channel_message.dart';
@@ -148,6 +149,25 @@ void main() {
});
});
test('flood transport code uses the scope key HMAC', () {
final scopeKey = floodScopeKeyForRegion('Europe');
final payload = Uint8List.fromList([0xA1, 0xB2, 0xC3]);
expect(
floodTransportCode(
scopeKey: scopeKey,
payloadType: 0x05,
payload: payload,
),
orderedEquals(
crypto.Hmac(
crypto.sha256,
scopeKey,
).convert([0x05, ...payload]).bytes.sublist(0, 2),
),
);
});
group('Contact.fromFrame — pathLen mapping', () {
test('pathLen == 0 → pathLength == 0 (direct, NOT flood)', () {
final frame = _buildContactFrame(pathLen: 0);
@@ -621,7 +641,8 @@ void main() {
test(
'ContactDiscoveryStore decodes and migrates legacy mode-encoded paths',
() async {
final store = ContactDiscoveryStore();
final store = ContactDiscoveryStore()
..setPublicKeyHex = 'AABBCCDDEEFF00112233';
final rawPath = Uint8List(64)
..[0] = 0x11
@@ -646,6 +667,8 @@ void main() {
expect(contacts, hasLength(1));
expect(contacts.first.pathLength, equals(1));
expect(contacts.first.path, equals(Uint8List.fromList([0x11, 0x22])));
expect(prefs.getString('discovered_contacts'), isNull);
expect(prefs.getString(store.keyFor), isNotNull);
},
);
});
+66
View File
@@ -0,0 +1,66 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/models/remote_node_stats.dart';
Uint8List _frame(int statsSize, void Function(ByteData stats) fill) {
final frame = Uint8List(RemoteNodeStats.payloadOffset + statsSize);
fill(ByteData.sublistView(frame, RemoteNodeStats.payloadOffset));
return frame;
}
void _fillCommon(ByteData d) {
d.setUint16(0, 4100, Endian.little);
d.setInt16(4, -110, Endian.little);
d.setUint32(16, 300, Endian.little);
d.setUint32(20, 3600, Endian.little);
d.setInt16(42, -22, Endian.little);
d.setUint16(46, 9, Endian.little);
}
void main() {
test('decodes repeater stats with rx airtime and recv errors', () {
final frame = _frame(56, (d) {
_fillCommon(d);
d.setUint32(48, 120, Endian.little);
d.setUint32(52, 7, Endian.little);
});
final stats = RemoteNodeStats.tryParse(frame, isRoom: false)!;
expect(stats.batteryMv, 4100);
expect(stats.noiseFloor, -110);
expect(stats.txAirSecs, 300);
expect(stats.uptimeSecs, 3600);
expect(stats.lastSnr, -5.5);
expect(stats.floodDups, 9);
expect(stats.rxAirSecs, 120);
expect(stats.recvErrors, 7);
expect(stats.posted, isNull);
});
test('older repeater frame without recv errors', () {
final frame = _frame(52, (d) {
_fillCommon(d);
d.setUint32(48, 120, Endian.little);
});
final stats = RemoteNodeStats.tryParse(frame, isRoom: false)!;
expect(stats.rxAirSecs, 120);
expect(stats.recvErrors, isNull);
});
test('decodes room server posted / pushed counters', () {
final frame = _frame(52, (d) {
_fillCommon(d);
d.setUint16(48, 42, Endian.little);
d.setUint16(50, 17, Endian.little);
});
final stats = RemoteNodeStats.tryParse(frame, isRoom: true)!;
expect(stats.posted, 42);
expect(stats.postPushes, 17);
expect(stats.rxAirSecs, isNull);
expect(stats.recvErrors, isNull);
});
test('rejects frames shorter than the common layout', () {
expect(RemoteNodeStats.tryParse(Uint8List(50), isRoom: false), isNull);
});
}