Add opt-in network map uploads and online map view
- Opt-in upload of header-only packet records and repeater/room server adverts to map.meshcoreopen.org about once a minute; payloads, channel and DM hashes and transport codes are stripped on the phone, locations snapped to a ~2 km grid, install ID rotates daily - One-time opt-in dialog on first radio connection; toggle and status in App settings - Local / Online / Both switch on the map screen with online repeater hexagons, links, add-to-contacts and manage - Node purchase banner on the connect screen (affiliate link, disclosed) - Privacy policy and network map documentation - Translations for all locales
This commit is contained in:
1 parent
d30744aa31
commit
bc14de586f
68 files changed
+6719
-76
No files matched your search
@@ -0,0 +1,191 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_api_client.dart';
|
||||
|
||||
const _cell = {
|
||||
'cell': '872830828ffffff',
|
||||
'lat': 33.45,
|
||||
'lon': -112.08,
|
||||
'boundary': [
|
||||
[33.46, -112.09],
|
||||
[33.45, -112.07],
|
||||
[33.44, -112.07],
|
||||
[33.43, -112.08],
|
||||
[33.44, -112.10],
|
||||
[33.45, -112.10],
|
||||
],
|
||||
};
|
||||
|
||||
final _repeater = {
|
||||
'key': 'AB' * 32,
|
||||
'name': 'Hilltop RPT',
|
||||
'type': 2,
|
||||
'cell': _cell,
|
||||
'locationSource': 'advert',
|
||||
'confidence': 1.0,
|
||||
'status': 'active',
|
||||
'confirmed': true,
|
||||
'sightings': 14,
|
||||
'firstSeen': '2026-10-01T12:00:00Z',
|
||||
'lastSeen': '2026-10-11T18:41:00Z',
|
||||
};
|
||||
|
||||
const _bbox = NetworkMapBbox(
|
||||
minLon: -113,
|
||||
minLat: 33,
|
||||
maxLon: -112,
|
||||
maxLat: 34,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('parses repeaters and sends bbox', () async {
|
||||
late Uri requested;
|
||||
final client = NetworkMapApiClient(
|
||||
client: MockClient((request) async {
|
||||
requested = request.url;
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'repeaters': [
|
||||
_repeater,
|
||||
{
|
||||
..._repeater,
|
||||
'key': 'cd' * 32,
|
||||
'name': null,
|
||||
'type': 3,
|
||||
'locationSource': 'estimated',
|
||||
'confidence': 0.4,
|
||||
'status': 'stale',
|
||||
'confirmed': false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final repeaters = await client.fetchRepeaters(_bbox);
|
||||
expect(requested.path, '/v1/repeaters');
|
||||
expect(requested.host, 'map.meshcoreopen.org');
|
||||
expect(
|
||||
requested.queryParameters['bbox'],
|
||||
'-113.00000,33.00000,-112.00000,34.00000',
|
||||
);
|
||||
expect(requested.queryParameters['includeUnconfirmed'], 'true');
|
||||
|
||||
expect(repeaters, hasLength(2));
|
||||
final first = repeaters.first;
|
||||
expect(first.key, 'ab' * 32);
|
||||
expect(first.name, 'Hilltop RPT');
|
||||
expect(first.type, 2);
|
||||
expect(first.cell.cell, '872830828ffffff');
|
||||
expect(first.cell.center.latitude, 33.45);
|
||||
expect(first.cell.boundary, hasLength(6));
|
||||
expect(first.cell.boundary.first.latitude, 33.46);
|
||||
expect(first.cell.boundary.first.longitude, -112.09);
|
||||
expect(first.status, NetworkMapRepeaterStatus.active);
|
||||
expect(first.confirmed, isTrue);
|
||||
expect(first.isEstimated, isFalse);
|
||||
expect(first.sightings, 14);
|
||||
expect(first.lastSeen, DateTime.utc(2026, 10, 11, 18, 41));
|
||||
|
||||
final second = repeaters.last;
|
||||
expect(second.name, isNull);
|
||||
expect(second.type, 3);
|
||||
expect(second.isEstimated, isTrue);
|
||||
expect(second.status, NetworkMapRepeaterStatus.stale);
|
||||
expect(second.confirmed, isFalse);
|
||||
});
|
||||
|
||||
test('parses a single repeater and treats 404 as unknown', () async {
|
||||
final client = NetworkMapApiClient(
|
||||
client: MockClient((request) async {
|
||||
if (request.url.path.endsWith('ab' * 32)) {
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
..._repeater,
|
||||
'links': 4,
|
||||
'coverageCells': 12,
|
||||
'reachableArea': '852830bffffffff',
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}
|
||||
return http.Response('{"error":"not_found"}', 404);
|
||||
}),
|
||||
);
|
||||
final repeater = (await client.fetchRepeater('AB' * 32))!;
|
||||
expect(repeater.links, 4);
|
||||
expect(repeater.coverageCells, 12);
|
||||
expect(repeater.reachableArea, '852830bffffffff');
|
||||
expect(await client.fetchRepeater('ef' * 32), isNull);
|
||||
});
|
||||
|
||||
test('parses links', () async {
|
||||
late Uri requested;
|
||||
final client = NetworkMapApiClient(
|
||||
client: MockClient((request) async {
|
||||
requested = request.url;
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'links': [
|
||||
{
|
||||
'a': 'ab' * 32,
|
||||
'b': 'cd' * 32,
|
||||
'aCell': _cell,
|
||||
'bCell': _cell,
|
||||
'packets': 120,
|
||||
'snr': 6.5,
|
||||
'confirmed': true,
|
||||
},
|
||||
{
|
||||
'a': 'ab' * 32,
|
||||
'b': 'ef' * 32,
|
||||
'aCell': _cell,
|
||||
'bCell': _cell,
|
||||
'packets': 3,
|
||||
'snr': null,
|
||||
'confirmed': false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final links = await client.fetchLinks(_bbox, window: '24h');
|
||||
expect(requested.path, '/v1/links');
|
||||
expect(requested.queryParameters['window'], '24h');
|
||||
expect(links, hasLength(2));
|
||||
expect(links.first.packets, 120);
|
||||
expect(links.first.snr, 6.5);
|
||||
expect(links.first.confirmed, isTrue);
|
||||
expect(links.first.aCell.boundary, hasLength(6));
|
||||
expect(links.last.snr, isNull);
|
||||
});
|
||||
|
||||
test('errors surface the status code', () async {
|
||||
final client = NetworkMapApiClient(
|
||||
client: MockClient((_) async => http.Response('bad', 400)),
|
||||
);
|
||||
expect(
|
||||
() => client.fetchRepeaters(_bbox),
|
||||
throwsA(isA<NetworkMapApiException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('bbox limit is 20 degrees per side', () {
|
||||
expect(_bbox.isWithinLimit, isTrue);
|
||||
expect(
|
||||
const NetworkMapBbox(
|
||||
minLon: -130,
|
||||
minLat: 30,
|
||||
maxLon: -105,
|
||||
maxLat: 40,
|
||||
).isWithinLimit,
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
import 'package:cryptography/cryptography.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/connector/raw_packet.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_records.dart';
|
||||
|
||||
const _packetFields = {
|
||||
't',
|
||||
'pt',
|
||||
'rt',
|
||||
'hw',
|
||||
'path',
|
||||
'snr',
|
||||
'rssi',
|
||||
'size',
|
||||
'h',
|
||||
'scoped',
|
||||
};
|
||||
|
||||
const _advertFields = {
|
||||
't',
|
||||
'key',
|
||||
'type',
|
||||
'name',
|
||||
'loc',
|
||||
'hidden',
|
||||
'snr',
|
||||
'rssi',
|
||||
'rt',
|
||||
'hw',
|
||||
'path',
|
||||
};
|
||||
|
||||
final _receivedAt = DateTime.utc(2026, 10, 11, 18, 41, 37);
|
||||
const _transportCodes = [0xDE, 0xAD, 0xBE, 0xEF];
|
||||
|
||||
String _hex(List<int> bytes) =>
|
||||
bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
||||
|
||||
/// `[0x88][snr*4][rssi][header][transport?][path_len][path][payload]`.
|
||||
Uint8List _rxFrame({
|
||||
required int payloadType,
|
||||
int routeType = 1,
|
||||
int snrQuarters = 29,
|
||||
int rssi = -92,
|
||||
int pathLenRaw = 0,
|
||||
List<int> path = const [],
|
||||
required List<int> payload,
|
||||
}) {
|
||||
return Uint8List.fromList([
|
||||
pushCodeLogRxData,
|
||||
snrQuarters & 0xFF,
|
||||
rssi & 0xFF,
|
||||
(payloadType << 2) | routeType,
|
||||
if (routeType == 0 || routeType == 3) ..._transportCodes,
|
||||
pathLenRaw,
|
||||
...path,
|
||||
...payload,
|
||||
]);
|
||||
}
|
||||
|
||||
/// Payload whose every 3-byte window is distinctive, so any leak of it into
|
||||
/// a record would show up as a substring.
|
||||
List<int> _secretPayload(int length) =>
|
||||
List<int>.generate(length, (i) => (0x5A + i * 37) & 0xFF);
|
||||
|
||||
Future<List<int>> _signedAdvertPayload({
|
||||
required SimpleKeyPair keyPair,
|
||||
required int type,
|
||||
double? lat,
|
||||
double? lon,
|
||||
String? name,
|
||||
bool corruptSignature = false,
|
||||
}) async {
|
||||
final publicKey = (await keyPair.extractPublicKey()).bytes;
|
||||
final timestamp = [0x10, 0x20, 0x30, 0x40];
|
||||
var flags = type;
|
||||
final appData = <int>[];
|
||||
if (lat != null && lon != null) {
|
||||
flags |= 0x10;
|
||||
final loc = ByteData(8)
|
||||
..setInt32(0, (lat * 1e6).round(), Endian.little)
|
||||
..setInt32(4, (lon * 1e6).round(), Endian.little);
|
||||
appData.addAll(loc.buffer.asUint8List());
|
||||
}
|
||||
if (name != null) {
|
||||
flags |= 0x80;
|
||||
appData.addAll(utf8.encode(name));
|
||||
}
|
||||
appData.insert(0, flags);
|
||||
final signature = await Ed25519().sign([
|
||||
...publicKey,
|
||||
...timestamp,
|
||||
...appData,
|
||||
], keyPair: keyPair);
|
||||
final sigBytes = List<int>.from(signature.bytes);
|
||||
if (corruptSignature) sigBytes[10] ^= 0x01;
|
||||
return [...publicKey, ...timestamp, ...sigBytes, ...appData];
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('packet records', () {
|
||||
test('every payload type yields header-only fields', () {
|
||||
for (var pt = 0; pt < 16; pt++) {
|
||||
for (final rt in [0, 1, 2, 3]) {
|
||||
final payload = _secretPayload(40);
|
||||
final frame = _rxFrame(
|
||||
payloadType: pt,
|
||||
routeType: rt,
|
||||
pathLenRaw: 0x42, // 2 hops, 2-byte hashes
|
||||
path: const [0xA1, 0xA2, 0xB1, 0xB2],
|
||||
payload: payload,
|
||||
);
|
||||
final record = buildPacketRecord(frame, _receivedAt)!;
|
||||
final json = record.toJson();
|
||||
expect(json.keys.toSet(), _packetFields);
|
||||
expect(json['pt'], pt);
|
||||
expect(json['rt'], rt);
|
||||
expect(json['hw'], 2);
|
||||
expect(json['path'], 'a1a2b1b2');
|
||||
expect(json['scoped'], rt == 0 || rt == 3);
|
||||
expect(json['h'], computePacketHash(pt, Uint8List.fromList(payload)));
|
||||
|
||||
// Nothing outside the hash may carry payload or transport bytes.
|
||||
final withoutHash = jsonEncode({...json}..remove('h'));
|
||||
expect(withoutHash, isNot(contains('dead')));
|
||||
expect(withoutHash, isNot(contains('beef')));
|
||||
for (var i = 0; i + 3 <= payload.length; i++) {
|
||||
expect(
|
||||
withoutHash,
|
||||
isNot(contains(_hex(payload.sublist(i, i + 3)))),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('group text: channel hash and MAC never appear', () {
|
||||
// [channel_hash][MAC x2][ciphertext]
|
||||
final payload = [0x7C, 0x3E, 0x91, ..._secretPayload(32)];
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(payloadType: payloadTypeGRPTXT, payload: payload),
|
||||
_receivedAt,
|
||||
)!;
|
||||
final json = jsonEncode(record.toJson()..remove('h'));
|
||||
expect(json, isNot(contains('7c')));
|
||||
expect(json, isNot(contains('3e91')));
|
||||
expect(record.path, '');
|
||||
});
|
||||
|
||||
test('direct message: source and destination hashes never appear', () {
|
||||
// [dest_hash][src_hash][MAC x2][ciphertext]
|
||||
final payload = [0xD7, 0x5C, 0x11, 0x22, ..._secretPayload(24)];
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeTXTMSG,
|
||||
routeType: 2,
|
||||
payload: payload,
|
||||
),
|
||||
_receivedAt,
|
||||
)!;
|
||||
final json = jsonEncode(record.toJson()..remove('h'));
|
||||
expect(json, isNot(contains('d7')));
|
||||
expect(json, isNot(contains('5c')));
|
||||
});
|
||||
|
||||
test('snr is reported in quarter-dB steps and rssi passes through', () {
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(payloadType: 2, snrQuarters: -23, rssi: -110, payload: [1]),
|
||||
_receivedAt,
|
||||
)!;
|
||||
expect(record.snr, -5.75);
|
||||
expect(record.rssi, -110);
|
||||
});
|
||||
|
||||
test('t is rounded down to the minute', () {
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(payloadType: 2, payload: [1]),
|
||||
_receivedAt,
|
||||
)!;
|
||||
expect(record.t % 60, 0);
|
||||
expect(
|
||||
record.t,
|
||||
DateTime.utc(2026, 10, 11, 18, 41).millisecondsSinceEpoch ~/ 1000,
|
||||
);
|
||||
});
|
||||
|
||||
test('size is the raw packet length rounded up to 16', () {
|
||||
expect(roundUpPacketSize(0), 0);
|
||||
expect(roundUpPacketSize(1), 16);
|
||||
expect(roundUpPacketSize(16), 16);
|
||||
expect(roundUpPacketSize(17), 32);
|
||||
expect(roundUpPacketSize(48), 48);
|
||||
expect(roundUpPacketSize(49), 64);
|
||||
expect(roundUpPacketSize(255), 256);
|
||||
|
||||
// header + path_len + 30 payload bytes = 32 raw bytes.
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(payloadType: 2, payload: _secretPayload(30)),
|
||||
_receivedAt,
|
||||
)!;
|
||||
expect(record.size, 32);
|
||||
final scoped = buildPacketRecord(
|
||||
_rxFrame(payloadType: 2, routeType: 0, payload: _secretPayload(30)),
|
||||
_receivedAt,
|
||||
)!;
|
||||
expect(scoped.size, 48); // + 4 transport bytes = 36
|
||||
});
|
||||
|
||||
test('hash matches the firmware packet hash', () {
|
||||
final payload = Uint8List.fromList(_secretPayload(20));
|
||||
final digest = crypto.sha256.convert([5, ...payload]).bytes;
|
||||
final record = buildPacketRecord(
|
||||
_rxFrame(payloadType: 5, payload: payload),
|
||||
_receivedAt,
|
||||
)!;
|
||||
expect(record.h, _hex(digest.sublist(0, 8)));
|
||||
expect(record.h, computePacketHash(5, payload));
|
||||
expect(RegExp(r'^[0-9a-f]{16}$').hasMatch(record.h), isTrue);
|
||||
});
|
||||
|
||||
test('unsupported hash width, oversized path and other frames dropped', () {
|
||||
// Hash width code 3 -> 4-byte hashes.
|
||||
expect(
|
||||
buildPacketRecord(
|
||||
_rxFrame(
|
||||
payloadType: 2,
|
||||
pathLenRaw: 0xC1,
|
||||
path: const [1, 2, 3, 4],
|
||||
payload: [1],
|
||||
),
|
||||
_receivedAt,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
// 22 hops of 3 bytes = 66 bytes > 64.
|
||||
expect(
|
||||
buildPacketRecord(
|
||||
_rxFrame(
|
||||
payloadType: 2,
|
||||
pathLenRaw: 0x80 | 22,
|
||||
path: List<int>.filled(66, 7),
|
||||
payload: [1],
|
||||
),
|
||||
_receivedAt,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
buildPacketRecord(Uint8List.fromList([0x80, 1, 2, 3]), _receivedAt),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
buildPacketRecord(
|
||||
Uint8List.fromList([pushCodeLogRxData, 1]),
|
||||
_receivedAt,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('advert records', () {
|
||||
late SimpleKeyPair keyPair;
|
||||
late String keyHex;
|
||||
|
||||
setUpAll(() async {
|
||||
keyPair = await Ed25519().newKeyPairFromSeed(List<int>.filled(32, 7));
|
||||
keyHex = _hex((await keyPair.extractPublicKey()).bytes);
|
||||
});
|
||||
|
||||
Future<Uint8List> advertFrame({
|
||||
required int type,
|
||||
double? lat,
|
||||
double? lon,
|
||||
String? name,
|
||||
bool corrupt = false,
|
||||
}) async {
|
||||
return _rxFrame(
|
||||
payloadType: payloadTypeADVERT,
|
||||
pathLenRaw: 0x01,
|
||||
path: const [0xA1],
|
||||
payload: await _signedAdvertPayload(
|
||||
keyPair: keyPair,
|
||||
type: type,
|
||||
lat: lat,
|
||||
lon: lon,
|
||||
name: name,
|
||||
corruptSignature: corrupt,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
test('verified repeater advert: snapped location, name, key', () async {
|
||||
final frame = await advertFrame(
|
||||
type: advTypeRepeater,
|
||||
lat: 33.448376,
|
||||
lon: -112.074036,
|
||||
name: 'Hilltop RPT',
|
||||
);
|
||||
final advert = (await buildAdvertRecord(frame, _receivedAt))!;
|
||||
final json = advert.toJson();
|
||||
expect(json.keys.toSet(), _advertFields);
|
||||
expect(json['key'], keyHex);
|
||||
expect(json['type'], 2);
|
||||
expect(json['name'], 'Hilltop RPT');
|
||||
expect(json['loc'], {'lat': 33.45, 'lon': -112.080261});
|
||||
expect(json['hidden'], false);
|
||||
expect(json['snr'], 7.25);
|
||||
expect(json['rssi'], -92);
|
||||
expect(json['rt'], 1);
|
||||
expect(json['hw'], 1);
|
||||
expect(json['path'], 'a1');
|
||||
expect(json['t'] % 60, 0);
|
||||
// Exact coordinates never leave the phone.
|
||||
expect(jsonEncode(json), isNot(contains('448376')));
|
||||
|
||||
// The same frame still produces a header-only packet record.
|
||||
expect(buildPacketRecord(frame, _receivedAt)!.pt, payloadTypeADVERT);
|
||||
});
|
||||
|
||||
test('room server adverts are uploaded too', () async {
|
||||
final frame = await advertFrame(type: advTypeRoom, name: 'Room');
|
||||
final advert = (await buildAdvertRecord(frame, _receivedAt))!;
|
||||
expect(advert.type, 3);
|
||||
expect(advert.loc, isNull);
|
||||
});
|
||||
|
||||
test('chat and sensor adverts are packet records only', () async {
|
||||
for (final type in [advTypeChat, advTypeSensor]) {
|
||||
final frame = await advertFrame(
|
||||
type: type,
|
||||
lat: 47.6,
|
||||
lon: -122.3,
|
||||
name: 'Alice',
|
||||
);
|
||||
expect(await buildAdvertRecord(frame, _receivedAt), isNull);
|
||||
final packet = buildPacketRecord(frame, _receivedAt)!;
|
||||
final json = jsonEncode(packet.toJson());
|
||||
expect(json, isNot(contains(keyHex.substring(0, 6))));
|
||||
expect(json, isNot(contains('Alice')));
|
||||
}
|
||||
});
|
||||
|
||||
test('names containing the no-entry sign are hidden', () async {
|
||||
final frame = await advertFrame(
|
||||
type: advTypeRepeater,
|
||||
lat: 47.6,
|
||||
lon: -122.3,
|
||||
name: 'Secret \u{1F6AB}',
|
||||
);
|
||||
final advert = (await buildAdvertRecord(frame, _receivedAt))!;
|
||||
final json = advert.toJson();
|
||||
expect(json['hidden'], true);
|
||||
expect(json['name'], isNull);
|
||||
expect(json['loc'], isNull);
|
||||
expect(json['key'], keyHex);
|
||||
});
|
||||
|
||||
test('adverts with a bad signature are dropped', () async {
|
||||
final frame = await advertFrame(
|
||||
type: advTypeRepeater,
|
||||
name: 'Forged',
|
||||
corrupt: true,
|
||||
);
|
||||
expect(await buildAdvertRecord(frame, _receivedAt), isNull);
|
||||
});
|
||||
|
||||
test('non-advert packets never produce adverts', () async {
|
||||
final frame = _rxFrame(payloadType: 5, payload: _secretPayload(120));
|
||||
expect(await buildAdvertRecord(frame, _receivedAt), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_upload_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
test('off by default and never listens without a connected radio', () async {
|
||||
final connector = MeshCoreConnector();
|
||||
final settings = AppSettingsService();
|
||||
await settings.loadSettings();
|
||||
var requests = 0;
|
||||
final service = NetworkMapUploadService(
|
||||
connector: connector,
|
||||
settings: settings,
|
||||
client: MockClient((_) async {
|
||||
requests++;
|
||||
return http.Response('{}', 202);
|
||||
}),
|
||||
);
|
||||
addTearDown(() {
|
||||
service.dispose();
|
||||
connector.dispose();
|
||||
});
|
||||
|
||||
expect(service.enabled, isFalse);
|
||||
expect(service.isActive, isFalse);
|
||||
|
||||
await service.setEnabled(true);
|
||||
expect(service.enabled, isTrue);
|
||||
expect(settings.settings.networkMapUploadEnabled, isTrue);
|
||||
expect(service.isActive, isFalse);
|
||||
expect(service.lastUploadAt, isNull);
|
||||
expect(service.packetsSentToday, 0);
|
||||
expect(requests, 0);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_records.dart';
|
||||
import 'package:meshcore_open/services/network_map/network_map_upload_service.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
NetworkMapPacketRecord _packet(int t) => NetworkMapPacketRecord(
|
||||
t: t,
|
||||
pt: 5,
|
||||
rt: 1,
|
||||
hw: 1,
|
||||
path: 'a1b2c3',
|
||||
snr: 7.25,
|
||||
rssi: -92,
|
||||
size: 48,
|
||||
h: '0123456789abcdef',
|
||||
scoped: false,
|
||||
);
|
||||
|
||||
NetworkMapAdvertRecord _advert(int t, String key) => NetworkMapAdvertRecord(
|
||||
t: t,
|
||||
key: key,
|
||||
type: 2,
|
||||
name: 'Hilltop RPT',
|
||||
loc: const NetworkMapLoc(33.47, -112.068234),
|
||||
hidden: false,
|
||||
snr: 9.5,
|
||||
rssi: -80,
|
||||
rt: 1,
|
||||
hw: 1,
|
||||
path: 'a1',
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('upload body', () {
|
||||
test('matches the contract field-for-field', () {
|
||||
final body = buildUploadBody(
|
||||
installId: 'AAAAAAAAAAAAAAAAAAAAAA',
|
||||
app: 'meshcore-open/9.5.0',
|
||||
loc: NetworkMapLoc.snapped(33.448376, -112.074036),
|
||||
preset: NetworkMapPreset.fromRadio(
|
||||
freqKhz: 910525,
|
||||
bwHz: 62500,
|
||||
sf: 7,
|
||||
cr: 5,
|
||||
),
|
||||
listenSeconds: 60,
|
||||
packets: [_packet(1791770400)],
|
||||
adverts: [_advert(1791770400, 'ab' * 32)],
|
||||
);
|
||||
final decoded =
|
||||
jsonDecode(jsonEncode(body)) as Map<String, dynamic>; // round trip
|
||||
expect(decoded, {
|
||||
'v': 1,
|
||||
'installId': 'AAAAAAAAAAAAAAAAAAAAAA',
|
||||
'token': null,
|
||||
'app': 'meshcore-open/9.5.0',
|
||||
'loc': {'lat': 33.45, 'lon': -112.080261},
|
||||
'preset': {'freqKhz': 910525, 'bwKhz': 62.5, 'sf': 7, 'cr': 5},
|
||||
'listenSeconds': 60,
|
||||
'packets': [
|
||||
{
|
||||
't': 1791770400,
|
||||
'pt': 5,
|
||||
'rt': 1,
|
||||
'hw': 1,
|
||||
'path': 'a1b2c3',
|
||||
'snr': 7.25,
|
||||
'rssi': -92,
|
||||
'size': 48,
|
||||
'h': '0123456789abcdef',
|
||||
'scoped': false,
|
||||
},
|
||||
],
|
||||
'adverts': [
|
||||
{
|
||||
't': 1791770400,
|
||||
'key': 'ab' * 32,
|
||||
'type': 2,
|
||||
'name': 'Hilltop RPT',
|
||||
'loc': {'lat': 33.47, 'lon': -112.068234},
|
||||
'hidden': false,
|
||||
'snr': 9.5,
|
||||
'rssi': -80,
|
||||
'rt': 1,
|
||||
'hw': 1,
|
||||
'path': 'a1',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('heartbeat has null loc/preset and empty lists', () {
|
||||
final body = buildUploadBody(
|
||||
installId: 'x',
|
||||
app: 'meshcore-open',
|
||||
loc: null,
|
||||
preset: null,
|
||||
listenSeconds: 900,
|
||||
packets: const [],
|
||||
adverts: const [],
|
||||
);
|
||||
expect(body['loc'], isNull);
|
||||
expect(body['preset'], isNull);
|
||||
expect(body['listenSeconds'], 600);
|
||||
expect(body['packets'], isEmpty);
|
||||
expect(body['adverts'], isEmpty);
|
||||
});
|
||||
|
||||
test('preset maps radio units and rejects unknown values', () {
|
||||
final preset = NetworkMapPreset.fromRadio(
|
||||
freqKhz: 869525,
|
||||
bwHz: 250000,
|
||||
sf: 11,
|
||||
cr: 1,
|
||||
)!;
|
||||
expect(preset.toJson(), {
|
||||
'freqKhz': 869525,
|
||||
'bwKhz': 250,
|
||||
'sf': 11,
|
||||
'cr': 5,
|
||||
});
|
||||
expect(
|
||||
NetworkMapPreset.fromRadio(
|
||||
freqKhz: 869525,
|
||||
bwHz: 41700,
|
||||
sf: 8,
|
||||
cr: 8,
|
||||
)!.bwKhz,
|
||||
41.7,
|
||||
);
|
||||
expect(
|
||||
NetworkMapPreset.fromRadio(freqKhz: 869525, bwHz: 12345, sf: 7, cr: 5),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
NetworkMapPreset.fromRadio(freqKhz: 50, bwHz: 62500, sf: 7, cr: 5),
|
||||
isNull,
|
||||
);
|
||||
expect(NetworkMapPreset.fromRadio(), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('install ID', () {
|
||||
test('random, 16 bytes, rotated every 24 h', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
var now = DateTime(2026, 10, 11, 12);
|
||||
final installId = NetworkMapInstallId(prefs, now: () => now);
|
||||
|
||||
final first = installId.current();
|
||||
expect(first.contains('='), isFalse);
|
||||
expect(RegExp(r'^[A-Za-z0-9_-]+$').hasMatch(first), isTrue);
|
||||
expect(base64Url.decode('$first=='), hasLength(16));
|
||||
|
||||
now = now.add(const Duration(hours: 23, minutes: 59));
|
||||
expect(installId.current(), first);
|
||||
|
||||
now = now.add(const Duration(minutes: 2));
|
||||
final second = installId.current();
|
||||
expect(second, isNot(first));
|
||||
expect(base64Url.decode('$second=='), hasLength(16));
|
||||
|
||||
now = now.add(const Duration(hours: 1));
|
||||
expect(installId.current(), second);
|
||||
});
|
||||
});
|
||||
|
||||
group('upload queue', () {
|
||||
final now = DateTime.utc(2026, 10, 11, 18, 41, 30);
|
||||
final minute = networkMapMinute(now);
|
||||
|
||||
test('batches respect the contract caps', () {
|
||||
final queue = NetworkMapUploadQueue();
|
||||
for (var i = 0; i < 2500; i++) {
|
||||
queue.addPacket(_packet(minute), now);
|
||||
}
|
||||
for (var i = 0; i < 250; i++) {
|
||||
queue.addAdvert(
|
||||
_advert(minute, i.toRadixString(16).padLeft(64, '0')),
|
||||
now,
|
||||
);
|
||||
}
|
||||
final batch = queue.peekBatch();
|
||||
expect(batch.packets, hasLength(2000));
|
||||
expect(batch.adverts, hasLength(200));
|
||||
|
||||
queue.remove(batch.packets.length, batch.adverts.length);
|
||||
final rest = queue.peekBatch();
|
||||
expect(rest.packets, hasLength(500));
|
||||
expect(rest.adverts, hasLength(50));
|
||||
});
|
||||
|
||||
test('one advert per key is kept', () {
|
||||
final queue = NetworkMapUploadQueue();
|
||||
queue.addAdvert(_advert(minute - 60, 'a' * 64), now);
|
||||
queue.addAdvert(_advert(minute, 'a' * 64), now);
|
||||
expect(queue.advertCount, 1);
|
||||
expect(queue.peekBatch().adverts.single.t, minute);
|
||||
});
|
||||
|
||||
test('drops data the server would reject or older than an hour', () {
|
||||
final queue = NetworkMapUploadQueue();
|
||||
queue.addPacket(_packet(minute), now);
|
||||
queue.addPacket(_packet(minute - 61 * 60), now);
|
||||
queue.addPacket(
|
||||
_packet(minute),
|
||||
now.subtract(const Duration(hours: 1, minutes: 1)),
|
||||
);
|
||||
queue.dropExpired(now);
|
||||
expect(queue.packetCount, 1);
|
||||
expect(queue.peekBatch().packets.single.t, minute);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user