import 'dart:convert'; import 'dart:typed_data'; import 'package:crypto/crypto.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:meshcore_open/services/ota/ota_release.dart'; const _target = 'Xiao_nrf52_repeater-ota'; const _base = 'fa1704ebb3f5a22d17914948a5e5252b8ab5d583d682ee0cda490b4f6b0fb6a1'; const _image = 'c7f1000f2ae05998a6b03c965929cd1ef6f6c6b1561d57fc491e6f5ef3d2ac84'; const _file = '$_target-v2.from-v1.mcfw'; Uint8List _package() { final b = Uint8List(320); b.setAll(0, ascii.encode('MCFW')); b[4] = 2; return b; } MockClient _server({Uint8List? served}) { final pkg = _package(); final body = served ?? pkg; final sums = '${sha256.convert(pkg)} $_file\n${'0' * 64} other.mcfw\n'; const dl = 'https://git.rfa.cz/dl/'; return MockClient((req) async { final u = req.url.toString(); if (u == kOtaReleaseApi) { return http.Response( jsonEncode({ 'tag_name': 'v2', 'assets': [ for (final n in ['manifest.json', 'SHA256SUMS', _file]) {'name': n, 'browser_download_url': '$dl$n'}, ], }), 200, ); } if (u == '${dl}manifest.json') { return http.Response( jsonEncode({ 'envs': { _target: { 'ota': { 'image_sha256': _image, 'deltas': { 'v1': {'file': _file, 'base_sha256': _base}, }, }, }, }, }), 200, ); } if (u == '${dl}SHA256SUMS') return http.Response(sums, 200); if (u == '$dl$_file') return http.Response.bytes(body, 200); return http.Response('', 404); }); } void main() { test('target from fw ver', () { expect(otaParseTarget('> target=$_target epoch=1794851405'), _target); expect(otaParseTarget('Unknown command'), isNull); }); test('the delta for the node image, checked against SHA256SUMS', () async { final m = await OtaRelease(client: _server()).find(_target, 'fa1704eb'); expect(m.tag, 'v2'); expect(m.package!.name, _file); expect(m.package!.delta, isTrue); }); test('a node on the release image is current', () async { final m = await OtaRelease(client: _server()).find(_target, 'c7f1000f'); expect(m.current, isTrue); }); test('no delta from an image too old', () async { final m = await OtaRelease(client: _server()).find(_target, 'deadbeef'); expect(m.current, isFalse); expect(m.package, isNull); }); test('a package that does not match SHA256SUMS is refused', () async { final bad = _package()..[300] = 1; expect( OtaRelease(client: _server(served: bad)).find(_target, 'fa1704eb'), throwsA(isA()), ); }); test('a target the release does not build', () async { expect( OtaRelease(client: _server()).find('Other-ota', 'fa1704eb'), throwsA(isA()), ); }); }