// The OTA protocol against the test vectors of its reference implementation, // module_utils/meshcore_ota.py of git.rfa.cz/tth-pub/meshcore-ansible // (tests/vectors/ota_vectors.json there; copy it over when it changes). import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:meshcore_open/services/ota/ota_protocol.dart'; Uint8List _bytes(String hex) => Uint8List.fromList([ for (var i = 0; i < hex.length; i += 2) int.parse(hex.substring(i, i + 2), radix: 16), ]); String _hex(List b) => b.map((x) => x.toRadixString(16).padLeft(2, '0')).join(); void main() { final v = jsonDecode(File('test/services/ota/ota_vectors.json').readAsStringSync()) as Map; final packages = [ for (final p in v['packages'] as List) OtaPackage(p['name'] as String, _bytes(p['hex'] as String)), ]; test('package manifest', () { for (final (i, p) in (v['packages'] as List).indexed) { expect(packages[i].format, p['format']); expect(packages[i].imageSha, p['image_sha']); expect(packages[i].baseSha, p['base_sha']); expect(packages[i].version, p['version']); } expect(() => OtaPackage('x', Uint8List(300)), throwsFormatException); }); test('pick', () { for (final c in v['pick'] as List) { final r = otaPick( packages, c['base'] as String, fullOk: c['full_ok'] as bool, ); expect(r.kind.name, c['kind'], reason: '${c['base']}'); expect(r.package?.name, c['package']); } }); test('crc16 and the binary write payload', () { for (final c in v['crc16'] as List) { expect(otaCrc16(_bytes(c['hex'] as String)), c['crc']); } for (final c in v['w_payload'] as List) { expect( _hex( otaWritePayload( c['sid'] as int, c['off'] as int, _bytes(c['chunk'] as String), ), ), c['hex'], ); } }); test('replies', () { for (final c in v['parse_status'] as List) { final r = otaParseStatus(c['text'] as String); expect(r == null ? null : [r.$1, r.$2], c['base']); } for (final c in v['parse_base'] as List) { expect(otaParseBase(c['text'] as String), c['sha8']); } for (final c in v['parse_missing'] as List) { final r = otaParseMissing(_bytes(c['hex'] as String)); expect(r?.$1, c['state']); expect(r?.$2, c['units']); } for (final c in v['text_missing'] as List) { expect(otaTextMissing(c['text'] as String), c['offsets']); } }); test('presets', () { for (final c in v['preset'] as List) { final p = OtaPreset.parse(c['text'] as String); expect([p.freq, p.bw, p.sf, p.cr], c['preset']); expect(p.toString(), c['fmt']); } for (final bad in v['preset_bad'] as List) { expect(() => OtaPreset.parse(bad as String), throwsFormatException); } for (final c in v['parse_radio'] as List) { final p = OtaPreset.fromReply(c['text'] as String); expect(p == null ? null : [p.freq, p.bw, p.sf, p.cr], c['preset']); } final p = OtaPreset.parse('869.525,62.5,7,5'); expect(p.freqField, 869525); // the companion frame: kHz expect(p.bwField, 62500); // and Hz }); test('airtime and frame lengths', () { for (final c in v['airtime'] as List) { expect( otaAirtime( c['n'] as int, c['sf'] as int, (c['bw'] as num).toDouble(), c['cr'] as int, ), closeTo((c['secs'] as num).toDouble(), 1e-9), ); } final f = v['frame_len'] as Map; for (final c in f['req'] as List) { expect(otaReqFrameLen(c['payload'] as int), c['len']); } for (final c in f['text'] as List) { expect(otaTextFrameLen(c['text'] as String), c['len']); } }); test('airtime bucket', () { var now = DateTime(2026, 11, 16); final b = OtaBucket(10, clock: () => now); expect(b.waitFor(360), 0); // a full hour's budget at once expect(b.waitFor(1), closeTo(10, 1e-6)); // then 0.1 s per second now = now.add(const Duration(seconds: 10)); expect(b.waitFor(1), 0); }); }