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
7 changed files with 481 additions and 92 deletions

View file

@ -15,7 +15,9 @@ import 'package:serverpod_client/serverpod_client.dart' as _isc;
/// Why the instance relays no firmware (ADR 0030, D40): `off` where its
/// operator turned relaying off, `invalid` for a band's figures it doesn't
/// take, `unknown` for a file it wasn't told of (or no longer holds),
/// `unreachable` where Polar's service didn't answer.
/// `unreachable` where Polar's service didn't answer or answered with
/// something else than an update, `tooOften` beyond 10 checks and
/// downloads a minute for a person (D55).
abstract class LoopFirmwareRefused
implements
_isc.SerializableException,

View file

@ -1,21 +1,9 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:halkyon_train_server/src/firmware/firmware_relay.dart';
import 'package:halkyon_train_server/src/generated/protocol.dart';
import 'package:serverpod/serverpod.dart';
/// The relay the endpoint uses: Polar's own service; tests put a fake.
FirmwareRelay firmwareRelay = FirmwareRelay(HttpPolarFirmwareService());
/// Whether the instance relays firmware, as its environment says; tests
/// set it.
bool Function() firmwareRelayed = () => firmwareRelayedBy(Platform.environment);
/// Whether [environment] has relaying on (D40): on unless
/// `LOOP_FIRMWARE_RELAY` is `off`.
bool firmwareRelayedBy(Map<String, String> environment) =>
environment['LOOP_FIRMWARE_RELAY']?.trim().toLowerCase() != 'off';
import 'package:serverpod_auth_idp_server/core.dart';
/// The Polar Loop's firmware through the instance (ADR 0030, D10, D40), for
/// the people signed in: the instance asks Polar in their place, so Polar
@ -25,7 +13,8 @@ class FirmwareEndpoint extends Endpoint {
bool get requireLogin => true;
/// Whether the instance relays firmware.
Future<bool> offered(Session session) async => firmwareRelayed();
Future<bool> offered(Session session) async =>
FirmwareRelay.of(session.serverpod).offered;
/// The update for the band of [uuid] (Polar's UUID made from its ID) at
/// [firmwareVersion] with [hardwareCode]; null where there is none.
@ -34,31 +23,21 @@ class FirmwareEndpoint extends Endpoint {
String uuid,
String firmwareVersion,
String hardwareCode,
) async {
_offeredOrRefused();
return await firmwareRelay.check(
uuid: uuid,
firmwareVersion: firmwareVersion,
hardwareCode: hardwareCode,
);
}
) => FirmwareRelay.of(session.serverpod).check(
session.authenticated!.authUserId,
uuid: uuid,
firmwareVersion: firmwareVersion,
hardwareCode: hardwareCode,
);
/// The file of [version] for bands of [hardwareCode], as a check named it.
Future<ByteData> download(
Session session,
String hardwareCode,
String version,
) async {
_offeredOrRefused();
return await firmwareRelay.download(
hardwareCode: hardwareCode,
version: version,
);
}
void _offeredOrRefused() {
if (!firmwareRelayed()) {
throw LoopFirmwareRefused(reason: 'off');
}
}
) => FirmwareRelay.of(session.serverpod).download(
session.authenticated!.authUserId,
hardwareCode: hardwareCode,
version: version,
);
}

View file

@ -1,9 +1,12 @@
import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:clock/clock.dart';
import 'package:halkyon_train_server/src/generated/protocol.dart';
import 'package:serverpod/serverpod.dart';
/// How long a firmware file is kept once fetched (D40).
const _kept = Duration(days: 1);
@ -23,10 +26,43 @@ abstract interface class PolarFirmwareService() {
Future<Uint8List> fetch(Uri file);
}
/// [PolarFirmwareService] at `firmware-management.polar.com`, with the
/// client name Polar's own SDK sends.
class HttpPolarFirmwareService() implements PolarFirmwareService {
static final _base = Uri.parse('https://firmware-management.polar.com');
/// Polar's firmware service didn't answer, or not as it does.
final class PolarFailed implements Exception {
const PolarFailed(this.reason);
final String reason;
@override
String toString() => 'PolarFailed: $reason';
}
/// [PolarFirmwareService] at `firmware-management.polar.com` (tests: [base]),
/// with the client name Polar's own SDK sends. Each wait is bounded by
/// [timeout], each answer by its size, and no redirect leaves `https`.
class HttpPolarFirmwareService implements PolarFirmwareService {
HttpPolarFirmwareService({
Uri? base,
this.timeout = const Duration(seconds: 10),
}) : base = base ?? Uri.parse('https://firmware-management.polar.com');
final Uri base;
/// The longest wait for a connection, an answer and each part of its body.
final Duration timeout;
/// The largest check answer: a version and an address are a few hundred
/// bytes.
static const maxAnswer = 64 * 1024;
/// The largest firmware file: the Loop's are a few megabytes, and each is
/// held in memory for a day (D40).
static const maxFile = 64 * 1024 * 1024;
/// The longest a file may take as a whole, however steadily it comes.
static const _wholeFile = Duration(minutes: 5);
/// The most redirects a file follows, each to `https` alone.
static const _redirects = 5;
@override
Future<({String version, Uri file})?> check({
@ -34,79 +70,155 @@ class HttpPolarFirmwareService() implements PolarFirmwareService {
required String firmwareVersion,
required String hardwareCode,
}) async {
final client = HttpClient();
final client = HttpClient()..connectionTimeout = timeout;
try {
final request = await client.postUrl(
_base.resolve('/api/v1/firmware-update/check'),
);
request.headers
..contentType = ContentType.json
..set(HttpHeaders.acceptHeader, 'application/json');
request.write(
jsonEncode({
'clientId': 'polar-sensor-data-collector-android',
'uuid': uuid,
'firmwareVersion': firmwareVersion,
'hardwareCode': hardwareCode,
}),
);
final response = await request.close();
final body = await utf8.decodeStream(response);
final request = await client
.postUrl(base.resolve('/api/v1/firmware-update/check'))
.timeout(timeout);
request
..followRedirects = false
..headers.contentType = ContentType.json
..headers.set(HttpHeaders.acceptHeader, 'application/json')
..write(
jsonEncode({
'clientId': 'polar-sensor-data-collector-android',
'uuid': uuid,
'firmwareVersion': firmwareVersion,
'hardwareCode': hardwareCode,
}),
);
final response = await request.close().timeout(timeout);
// No update is answered with no content (or not found).
if (response.statusCode == HttpStatus.noContent ||
response.statusCode == HttpStatus.notFound) {
return null;
}
if (response.statusCode != HttpStatus.ok) {
throw HttpException('firmware check ${response.statusCode}');
throw PolarFailed('check ${response.statusCode}');
}
final body = utf8.decode(
await _read(response, maxAnswer).timeout(timeout),
);
// An answer without both, or naming a file not over https, is a format
// Polar changed: not "no update".
return switch (jsonDecode(body)) {
{'version': final String version, 'fileUrl': final String file}
when Uri.tryParse(file)?.isScheme('https') ?? false =>
(version: version, file: Uri.parse(file)),
_ => null,
_ => throw const PolarFailed('check answer'),
};
} finally {
client.close();
client.close(force: true);
}
}
/// The file at [file], which [check] named over `https`, following its
/// redirects only where they stay on `https`.
@override
Future<Uint8List> fetch(Uri file) async {
final client = HttpClient();
final client = HttpClient()..connectionTimeout = timeout;
try {
final response = await (await client.getUrl(file)).close();
if (response.statusCode != HttpStatus.ok) {
throw HttpException('firmware file ${response.statusCode}');
var at = file;
for (var redirects = 0; ; redirects++) {
final request = await client.getUrl(at).timeout(timeout);
request.followRedirects = false;
final response = await request.close().timeout(timeout);
if (response.isRedirect) {
final location = response.headers.value(HttpHeaders.locationHeader);
final next = location == null ? null : at.resolve(location);
if (next == null ||
!next.isScheme('https') ||
redirects >= _redirects) {
throw const PolarFailed('file redirect');
}
at = next;
continue;
}
if (response.statusCode != HttpStatus.ok) {
throw PolarFailed('file ${response.statusCode}');
}
return await _read(response, maxFile).timeout(_wholeFile);
}
final bytes = BytesBuilder(copy: false);
await response.forEach(bytes.add);
return bytes.takeBytes();
} finally {
client.close();
client.close(force: true);
}
}
/// [response]'s body, refused beyond [max] bytes (as soon as its length
/// says so) and where a part is longer than [timeout] in coming.
Future<Uint8List> _read(HttpClientResponse response, int max) async {
if (response.contentLength > max) {
throw const PolarFailed('too large');
}
final bytes = BytesBuilder(copy: false);
await for (final chunk in response.timeout(timeout)) {
if (bytes.length + chunk.length > max) {
throw const PolarFailed('too large');
}
bytes.add(chunk);
}
return bytes.takeBytes();
}
}
/// Whether [environment] has relaying on (D40): on unless
/// `LOOP_FIRMWARE_RELAY` is `off`.
bool firmwareRelayedBy(Map<String, String> environment) =>
environment['LOOP_FIRMWARE_RELAY']?.trim().toLowerCase() != 'off';
/// The instance relaying Polar's firmware for its people (D40): it asks
/// Polar in their place and fetches each file once for every band of its
/// hardware, keeping it in memory for a day.
class FirmwareRelay(final PolarFirmwareService _polar) {
/// Polar in their place, as often as [perMinute] allows, and fetches each
/// file once for every band of its hardware, keeping it in memory for a day.
class FirmwareRelay {
FirmwareRelay(this._polar, {this.offered = true});
/// The relay of [pod], made the first time it is asked for: Polar's own
/// service, on unless the operator's `LOOP_FIRMWARE_RELAY` is `off`. Tests
/// set their own.
factory FirmwareRelay.of(Serverpod pod) => _ofPod[pod] ??= FirmwareRelay(
HttpPolarFirmwareService(),
offered: firmwareRelayedBy(Platform.environment),
);
static final _ofPod = Expando<FirmwareRelay>('FirmwareRelay');
/// Makes [relay] the one of [pod].
static void use(Serverpod pod, FirmwareRelay relay) => _ofPod[pod] = relay;
final PolarFirmwareService _polar;
/// Whether the instance relays firmware; off: everything is refused.
final bool offered;
/// The most checks and downloads a person may ask for in a minute: Train
/// checks once a day and when asked (D40), and downloads what it found, so
/// a few a minute are a person tapping *Check for updates* again; more
/// would have the instance ask Polar, or hand out files, for anyone.
static const perMinute = 10;
/// The files Polar named, by hardware and version: where each is, and
/// the file once fetched.
final _files = <String, ({Uri file, Uint8List? bytes, DateTime at})>{};
/// The update for a band; null where Polar has none. Throws
/// [LoopFirmwareRefused] for figures it doesn't take, or where Polar
/// didn't answer.
Future<LoopFirmware?> check({
/// The fetches under way, by hardware and version, which those
/// downloading the same file meanwhile wait for.
final _fetching = <String, Future<Uint8List>>{};
final _asked = HashMap<UuidValue, Queue<DateTime>>();
/// The update for [person]'s band; null where Polar has none. Throws
/// [LoopFirmwareRefused] where relaying is off, for figures it doesn't
/// take, beyond [perMinute], or where Polar didn't answer as it does.
Future<LoopFirmware?> check(
UuidValue person, {
required String uuid,
required String firmwareVersion,
required String hardwareCode,
}) async {
_offeredOrRefused();
if (!_valid(uuid) || !_valid(firmwareVersion) || !_valid(hardwareCode)) {
throw LoopFirmwareRefused(reason: 'invalid');
}
_count(person);
final ({String version, Uri file})? update;
try {
update = await _polar.check(
@ -129,13 +241,17 @@ class FirmwareRelay(final PolarFirmwareService _polar) {
return LoopFirmware(version: update.version);
}
/// The file of [version] for [hardwareCode], as a check named it; fetched
/// once and kept for a day. Throws [LoopFirmwareRefused] for one no check
/// named, or where Polar didn't answer.
Future<ByteData> download({
/// The file of [version] for [hardwareCode], as a check named it, for
/// [person]; fetched once, however many ask meanwhile, and kept for a day.
/// Throws [LoopFirmwareRefused] where relaying is off, beyond
/// [perMinute], for one no check named, or where Polar didn't answer.
Future<ByteData> download(
UuidValue person, {
required String hardwareCode,
required String version,
}) async {
_offeredOrRefused();
_count(person);
_forgetOld();
final key = _key(hardwareCode, version);
final known = _files[key];
@ -147,14 +263,54 @@ class FirmwareRelay(final PolarFirmwareService _polar) {
}
final Uint8List bytes;
try {
bytes = await _polar.fetch(known.file);
bytes = await (_fetching[key] ??= _fetch(key, known.file));
} on Object {
throw LoopFirmwareRefused(reason: 'unreachable');
}
_files[key] = (file: known.file, bytes: bytes, at: clock.now());
return ByteData.sublistView(bytes);
}
/// Fetches [file] and keeps it under [key], unless a check named another
/// file there meanwhile.
Future<Uint8List> _fetch(String key, Uri file) async {
try {
final bytes = await _polar.fetch(file);
if (_files[key]?.file == file) {
_files[key] = (file: file, bytes: bytes, at: clock.now());
}
return bytes;
} finally {
unawaited(_fetching.remove(key));
}
}
void _offeredOrRefused() {
if (!offered) {
throw LoopFirmwareRefused(reason: 'off');
}
}
/// Counts a check or download of [person]'s, refusing it beyond
/// [perMinute] in the last minute.
void _count(UuidValue person) {
final now = clock.now();
final asked = _asked.putIfAbsent(person, Queue.new);
while (asked.isNotEmpty &&
now.difference(asked.first) >= const Duration(minutes: 1)) {
asked.removeFirst();
}
if (asked.length >= perMinute) {
throw LoopFirmwareRefused(reason: 'tooOften');
}
asked.addLast(now);
// Those who asked nothing for a minute are forgotten.
_asked.removeWhere(
(_, times) =>
times.isEmpty ||
now.difference(times.last) >= const Duration(minutes: 1),
);
}
void _forgetOld() {
final before = clock.now().subtract(_kept);
_files.removeWhere((_, file) => file.at.isBefore(before));

View file

@ -1,7 +1,9 @@
### Why the instance relays no firmware (ADR 0030, D40): `off` where its
### operator turned relaying off, `invalid` for a band's figures it doesn't
### take, `unknown` for a file it wasn't told of (or no longer holds),
### `unreachable` where Polar's service didn't answer.
### `unreachable` where Polar's service didn't answer or answered with
### something else than an update, `tooOften` beyond 10 checks and
### downloads a minute for a person (D55).
exception: LoopFirmwareRefused
fields:
reason: String

View file

@ -15,7 +15,9 @@ import 'package:serverpod/serverpod.dart' as _is;
/// Why the instance relays no firmware (ADR 0030, D40): `off` where its
/// operator turned relaying off, `invalid` for a band's figures it doesn't
/// take, `unknown` for a file it wasn't told of (or no longer holds),
/// `unreachable` where Polar's service didn't answer.
/// `unreachable` where Polar's service didn't answer or answered with
/// something else than an update, `tooOften` beyond 10 checks and
/// downloads a minute for a person (D55).
abstract class LoopFirmwareRefused
implements
_is.SerializableException,

View file

@ -1,8 +1,8 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:clock/clock.dart';
import 'package:halkyon_train_server/src/firmware/firmware_endpoint.dart';
import 'package:halkyon_train_server/src/firmware/firmware_relay.dart';
import 'package:halkyon_train_server/src/generated/protocol.dart';
import 'package:serverpod/serverpod.dart';
@ -21,6 +21,9 @@ class _FakePolar() implements PolarFirmwareService {
int fetches = 0;
bool down = false;
/// Where set, fetches wait for it.
Completer<void>? fetching;
@override
Future<({String version, Uri file})?> check({
required String uuid,
@ -37,13 +40,18 @@ class _FakePolar() implements PolarFirmwareService {
@override
Future<Uint8List> fetch(Uri file) async {
fetches++;
await fetching?.future;
return Uint8List.fromList([1, 2, 3]);
}
}
Matcher _refused(String reason) => throwsA(
isA<LoopFirmwareRefused>().having((e) => e.reason, 'reason', reason),
);
/// The Loop's firmware through the instance (ADR 0030, D10, D40): for the
/// people signed in, unless the operator turned it off; each file fetched
/// once for every band of its hardware.
/// people signed in, unless the operator turned it off, within a limit a
/// minute (D11); each file fetched once for every band of its hardware.
void main() {
withServerpod('Relaying the Loop firmware', (sessionBuilder, endpoints) {
late _FakePolar polar;
@ -53,11 +61,19 @@ void main() {
{},
),
);
final other = sessionBuilder.copyWith(
authentication: AuthenticationOverride.authenticationInfo(
const Uuid().v7(),
{},
),
);
void useRelay(FirmwareRelay relay) =>
FirmwareRelay.use(sessionBuilder.build().serverpod, relay);
setUp(() {
polar = _FakePolar();
firmwareRelay = FirmwareRelay(polar);
firmwareRelayed = () => true;
useRelay(FirmwareRelay(polar));
});
test('names an update to someone signed in, and hands its file', () async {
@ -91,6 +107,48 @@ void main() {
expect(polar.fetches, 1);
});
test(
'fetches a file once for those downloading it at the same time',
() async {
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
polar.fetching = Completer();
final first = endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
final second = endpoints.firmware.download(other, 'HW-1', '3.1.2');
await pumpEventQueue();
polar.fetching!.complete();
for (final file in await Future.wait([first, second])) {
expect(Uint8List.sublistView(file), [1, 2, 3]);
}
expect(polar.fetches, 1);
},
);
test('refuses a person beyond the checks and downloads of a minute, not '
'others, and relays again a minute later', () async {
var now = DateTime.utc(2026, 10, 28, 12);
await withClock(Clock(() => now), () async {
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
for (var i = 1; i < FirmwareRelay.perMinute; i++) {
await endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
}
await expectLater(
endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1'),
_refused('tooOften'),
);
await expectLater(
endpoints.firmware.download(signedIn, 'HW-1', '3.1.2'),
_refused('tooOften'),
);
expect(polar.checks, 1);
await endpoints.firmware.check(other, 'uuid-2', '3.0.0', 'HW-1');
now = now.add(const Duration(minutes: 1));
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
});
});
test('hands no file a check never named', () async {
await expectLater(
endpoints.firmware.download(signedIn, 'HW-9', '9.9.9'),
@ -120,7 +178,7 @@ void main() {
expect(polar.checks, 0);
});
test("says Polar didn't answer", () async {
test("says Polar didn't answer, or not as it does", () async {
polar.down = true;
await expectLater(
@ -138,7 +196,7 @@ void main() {
test(
'relays nothing where the operator turned it off, and says so',
() async {
firmwareRelayed = () => false;
useRelay(FirmwareRelay(polar, offered: false));
expect(await endpoints.firmware.offered(signedIn), isFalse);
await expectLater(
@ -185,23 +243,26 @@ void main() {
test('a file is kept a day, then fetched again after a new check', () async {
final polar = _FakePolar();
final relay = FirmwareRelay(polar);
final person = const Uuid().v7obj();
var now = DateTime.utc(2026, 10, 28, 9);
await withClock(Clock(() => now), () async {
await relay.check(
person,
uuid: 'uuid-1',
firmwareVersion: '3.0.0',
hardwareCode: 'HW-1',
);
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
await relay.download(person, hardwareCode: 'HW-1', version: '3.1.2');
now = now.add(const Duration(hours: 25));
// Checked again a day on, before any download: fetched anew.
await relay.check(
person,
uuid: 'uuid-1',
firmwareVersion: '3.0.0',
hardwareCode: 'HW-1',
);
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
await relay.download(person, hardwareCode: 'HW-1', version: '3.1.2');
});
expect(polar.fetches, 2);

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']);
});
}