fix(train): the firmware relay's calls to Polar time out, are bounded and limited per person (ADR 0030, D55)

Like BRouter's and Photon's: timeouts on connecting and reading, a check answer up to 64 KB and a file up to 64 MB, no redirect but to https, one fetch per file at a time, and 10 checks and downloads a minute for a person (tooOften). An answer that names no update properly is unreachable, no longer no update. The relay is the pod's, as routing's and search's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-30 12:55:42 +01:00
commit 0240a80faf
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -0,0 +1,187 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:halkyon_train_server/src/firmware/firmware_relay.dart';
import 'package:test/test.dart';
/// Polar's firmware service on the loopback, answering each request with
/// [answer].
Future<Uri> _polar(Future<void> Function(HttpRequest request) answer) async {
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
server.listen(answer);
addTearDown(() => server.close(force: true));
return Uri.parse('http://127.0.0.1:${server.port}/');
}
/// Answers [request] with [status] and [body] as JSON.
Future<void> _json(HttpRequest request, Object? body, {int? status}) async {
request.response
..statusCode = status ?? HttpStatus.ok
..headers.contentType = ContentType.json
..write(jsonEncode(body));
await request.response.close();
}
Future<({String version, Uri file})?> _check(HttpPolarFirmwareService polar) =>
polar.check(uuid: 'uuid-1', firmwareVersion: '3.0.0', hardwareCode: 'HW-1');
final _failed = throwsA(isA<PolarFailed>());
/// Polar's firmware service as Train's server asks it (ADR 0030, D10, D40):
/// an update only where Polar names one in the format it does, every wait
/// and every answer bounded, no file but over `https`.
void main() {
test('asks the check with the SDK client name and the band, and reads '
'the version and the file', () async {
final asked = <(String, Object?)>[];
final base = await _polar((request) async {
asked.add((
request.uri.path,
jsonDecode(await utf8.decodeStream(request)),
));
await _json(request, {
'version': '3.1.2',
'fileUrl': 'https://files.example/loop-3.1.2.zip',
});
});
final update = await _check(HttpPolarFirmwareService(base: base));
expect(update?.version, '3.1.2');
expect(update?.file, Uri.parse('https://files.example/loop-3.1.2.zip'));
expect(asked.single.$1, '/api/v1/firmware-update/check');
expect(asked.single.$2, {
'clientId': 'polar-sensor-data-collector-android',
'uuid': 'uuid-1',
'firmwareVersion': '3.0.0',
'hardwareCode': 'HW-1',
});
});
test('names no update where Polar answers none', () async {
for (final status in [HttpStatus.noContent, HttpStatus.notFound]) {
final base = await _polar((request) async {
request.response.statusCode = status;
await request.response.close();
});
expect(await _check(HttpPolarFirmwareService(base: base)), isNull);
}
});
test('fails, rather than naming no update, on an answer without a '
'version or a file, or with a file not over https', () async {
for (final body in [
{'version': '3.1.2'},
{'fileUrl': 'https://files.example/loop-3.1.2.zip'},
{'version': '3.1.2', 'fileUrl': 'http://files.example/loop-3.1.2.zip'},
'not an update',
]) {
final base = await _polar((request) => _json(request, body));
await expectLater(_check(HttpPolarFirmwareService(base: base)), _failed);
}
});
test(
'fails on another status, and follows no redirect of the check',
() async {
for (final status in [HttpStatus.badGateway, HttpStatus.found]) {
final base = await _polar((request) async {
request.response
..statusCode = status
..headers.set(
HttpHeaders.locationHeader,
'https://elsewhere.example/',
);
await request.response.close();
});
await expectLater(
_check(HttpPolarFirmwareService(base: base)),
_failed,
);
}
},
);
test('fails on a check answer beyond its size', () async {
final base = await _polar((request) async {
// Chunked, so that no length says so beforehand.
request.response
..statusCode = HttpStatus.ok
..write('"${'x' * HttpPolarFirmwareService.maxAnswer}"');
await request.response.close();
});
await expectLater(_check(HttpPolarFirmwareService(base: base)), _failed);
});
test('gives up on a Polar that never answers', () async {
final base = await _polar((request) async {
// Holds the request open, answering nothing.
});
await expectLater(
_check(
HttpPolarFirmwareService(
base: base,
timeout: const Duration(milliseconds: 200),
),
),
throwsA(isA<TimeoutException>()),
);
});
test('fetches a file', () async {
final base = await _polar((request) async {
request.response.add([1, 2, 3]);
await request.response.close();
});
expect(
await HttpPolarFirmwareService().fetch(base.resolve('loop.zip')),
[1, 2, 3],
);
});
test(
'fails on a file whose length is beyond its size, before reading it',
() async {
final base = await _polar((request) async {
request.response
..contentLength = HttpPolarFirmwareService.maxFile + 1
..add([1, 2, 3]);
// The rest never comes: the client goes before it would.
try {
await request.response.close();
} on HttpException {
// The client went first.
}
});
await expectLater(
HttpPolarFirmwareService().fetch(base.resolve('loop.zip')),
_failed,
);
},
);
test('follows no redirect of a file away from https', () async {
final asked = <String>[];
final base = await _polar((request) async {
asked.add(request.uri.path);
request.response
..statusCode = HttpStatus.found
..headers.set(HttpHeaders.locationHeader, '/elsewhere.zip');
await request.response.close();
});
await expectLater(
HttpPolarFirmwareService().fetch(base.resolve('loop.zip')),
_failed,
);
expect(asked, ['/loop.zip']);
});
}