mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
Follow-ups to the mobile diagnostic reporting work in
docs/mobile-diagnostic-reporting.md.
## Changes
- **Install ID repair:** if the install ID file holds something that
isn't text, the app now replaces it with a new ID, as it already does
for an empty file. Before, every report went out without an install ID.
A read that fails (for example, no file descriptor free) still keeps the
file, so a passing error never replaces a good ID.
- **Clock-window refusals are not reported as errors:** the spec treats
relay refusals for a timestamp outside the relay's clock window as
expected (counting them is still pending). The app only recognized one
of the relay's wordings ("timestamp too far from server time"), so
refusals for canvas events, relay-admin commands, leave requests and
observer frames were reported as invalid requests. The five wordings are
matched from the start of the reason, so unrelated `invalid:` reasons
that mention a timestamp, such as the feedback timestamp overflow or one
that echoes a value, are still reported.
- **Diagnostics card:** rows get subtitles, and the divider indent
matches the neighbouring Settings cards.
- **Comments:** explain why push stop gates its current-identity request
on the community key, and correct a test comment about who drops a
repeated error object.
## Validation
New tests cover:
- member attribution on relay sessions, including a missing key and an
answer that arrives after the member changed;
- push stop reports while signed out;
- reporter wiring for hosted admission and the self-hosted probe;
- frames the socket can't decode;
- a table of relay refusal reasons, each either reported or not, for
both `OK` and `CLOSED`;
- an install ID file whose reads fail keeps its ID.
Full-app run on an iPhone 17 Pro simulator at 61b04f6: startup, channel
list, sending and reacting, Settings, the Diagnostics card and Send test
report, and install ID repair (a file that isn't valid UTF-8 and an
empty file each got a new ID that stayed the same on the next launch).
Not covered: a relay clock-window refusal, which the live relay would
not produce.
## Diagnostics card
| Before | After |
| --- | --- |
| 
| 
|
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
228 lines
7.2 KiB
Dart
228 lines
7.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:buzz/shared/hosted/account_identity_store.dart';
|
|
import 'package:buzz/shared/hosted/builderlab_account.dart';
|
|
import 'package:buzz/shared/hosted/hosted_backup_service.dart';
|
|
import 'package:buzz/shared/hosted/hpke_backup.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
class FakeAccount extends BuilderlabAccountClient {
|
|
FakeAccount(this.envelope);
|
|
final Map<String, dynamic> envelope;
|
|
int enrollments = 0;
|
|
final uploads = <String>[];
|
|
bool fail = true;
|
|
bool reject = false;
|
|
bool rejectEnrollment = false;
|
|
String subject = 'subject';
|
|
@override
|
|
BuilderlabAccount get account =>
|
|
BuilderlabAccount(subject: subject, expiresAt: DateTime(2100));
|
|
@override
|
|
Future<Map<String, dynamic>> post(
|
|
String path,
|
|
Map<String, dynamic> body, {
|
|
String? expectedSubject,
|
|
String? signer,
|
|
}) async {
|
|
if (path.endsWith('/enrollment')) {
|
|
enrollments++;
|
|
if (rejectEnrollment) throw AccountFailure.rejected;
|
|
return envelope;
|
|
}
|
|
uploads.add(jsonEncode(body));
|
|
expect(
|
|
await const FlutterSecureStorage().read(
|
|
key: HostedBackupService.pendingKey(
|
|
BuilderlabAccountClient.origin.origin,
|
|
subject,
|
|
envelope['nostr_pubkey'] as String,
|
|
),
|
|
),
|
|
isNotNull,
|
|
);
|
|
if (reject) throw AccountFailure.rejected;
|
|
if (fail) throw AccountFailure.unavailable;
|
|
return {'stored': true, 'backup_id': envelope['backup_id']};
|
|
}
|
|
}
|
|
|
|
/// An organization account whose enrollment never answers.
|
|
class StalledAccount extends BuilderlabAccountClient {
|
|
StalledAccount()
|
|
: super(accountOrigin: Uri.parse('https://block.example.com'));
|
|
final enrolling = Completer<void>();
|
|
@override
|
|
BuilderlabAccount get account =>
|
|
BuilderlabAccount(subject: 'member', expiresAt: DateTime(2100));
|
|
@override
|
|
Future<Map<String, dynamic>> post(
|
|
String path,
|
|
Map<String, dynamic> body, {
|
|
String? expectedSubject,
|
|
String? signer,
|
|
}) {
|
|
if (!enrolling.isCompleted) enrolling.complete();
|
|
return Completer<Map<String, dynamic>>().future;
|
|
}
|
|
}
|
|
|
|
class FakeHpke extends HpkeBackup {
|
|
FakeHpke(this.envelope);
|
|
final Map<String, dynamic> envelope;
|
|
int calls = 0;
|
|
@override
|
|
Future<Map<String, dynamic>> seal({
|
|
required Map<String, dynamic> enrollment,
|
|
required String secretHex,
|
|
}) async {
|
|
calls++;
|
|
return envelope;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
late Map<String, dynamic> envelope;
|
|
setUp(() {
|
|
FlutterSecureStorage.setMockInitialValues({});
|
|
final fixture =
|
|
jsonDecode(
|
|
File(
|
|
'../desktop/src-tauri/src/testdata/hpke_nsec_backup_v1.json',
|
|
).readAsStringSync(),
|
|
)
|
|
as Map<String, dynamic>;
|
|
envelope = Map<String, dynamic>.from(fixture['envelope'] as Map);
|
|
});
|
|
test(
|
|
'uncertain upload replays persisted envelope after restart and receipt prevents further upload',
|
|
() async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
final account = FakeAccount(envelope);
|
|
final hpke = FakeHpke(envelope);
|
|
await expectLater(
|
|
HostedBackupService(account, identity, hpke: hpke).backup(),
|
|
throwsA(AccountFailure.unavailable),
|
|
);
|
|
expect((await identity.read())!.backup, isNull);
|
|
account.fail = false;
|
|
await HostedBackupService(
|
|
account,
|
|
AccountIdentityStore(),
|
|
hpke: hpke,
|
|
).backup();
|
|
expect(account.enrollments, 1);
|
|
expect(hpke.calls, 1);
|
|
expect(account.uploads, hasLength(2));
|
|
expect(account.uploads.first, account.uploads.last);
|
|
expect((await identity.read())!.backup!.backupId, envelope['backup_id']);
|
|
await HostedBackupService(account, identity, hpke: hpke).backup();
|
|
expect(account.uploads, hasLength(2));
|
|
},
|
|
);
|
|
test(
|
|
'a stalled organization backup does not hold up the hosted one',
|
|
() async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
final stalled = StalledAccount();
|
|
unawaited(
|
|
HostedBackupService(
|
|
stalled,
|
|
identity,
|
|
hpke: FakeHpke(envelope),
|
|
).backup(),
|
|
);
|
|
await stalled.enrolling.future;
|
|
final account = FakeAccount(envelope)..fail = false;
|
|
await HostedBackupService(
|
|
account,
|
|
identity,
|
|
hpke: FakeHpke(envelope),
|
|
).backup().timeout(const Duration(seconds: 5));
|
|
expect(account.uploads, hasLength(1));
|
|
},
|
|
);
|
|
test('unreadable pending ciphertext blocks enrollment and upload', () async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
await const FlutterSecureStorage().write(
|
|
key: HostedBackupService.pendingKey(
|
|
BuilderlabAccountClient.origin.origin,
|
|
'subject',
|
|
envelope['nostr_pubkey'] as String,
|
|
),
|
|
value: 'invalid',
|
|
);
|
|
final account = FakeAccount(envelope);
|
|
await expectLater(
|
|
HostedBackupService(account, identity).backup(),
|
|
throwsA(HostedBackupFailure.storage),
|
|
);
|
|
expect(account.enrollments, 0);
|
|
expect(account.uploads, isEmpty);
|
|
});
|
|
test(
|
|
'definitive rejection permits only one re-enrollment per attempt',
|
|
() async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
final account = FakeAccount(envelope)..reject = true;
|
|
await expectLater(
|
|
HostedBackupService(
|
|
account,
|
|
identity,
|
|
hpke: FakeHpke(envelope),
|
|
).backup(),
|
|
throwsA(HostedBackupFailure.rejected),
|
|
);
|
|
expect(account.enrollments, 2);
|
|
expect(account.uploads, hasLength(2));
|
|
expect((await identity.read())!.backup, isNull);
|
|
},
|
|
);
|
|
test('enrollment rejection is terminal without upload', () async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
final account = FakeAccount(envelope)..rejectEnrollment = true;
|
|
await expectLater(
|
|
HostedBackupService(account, identity).backup(),
|
|
throwsA(HostedBackupFailure.unavailable),
|
|
);
|
|
expect(account.enrollments, 1);
|
|
expect(account.uploads, isEmpty);
|
|
});
|
|
test(
|
|
'another subject pending envelope neither blocks nor gets replaced',
|
|
() async {
|
|
final identity = AccountIdentityStore();
|
|
await identity.create(nostr.Keys('42' * 32).nsec);
|
|
final oldKey = HostedBackupService.pendingKey(
|
|
BuilderlabAccountClient.origin.origin,
|
|
'other',
|
|
envelope['nostr_pubkey'] as String,
|
|
);
|
|
await const FlutterSecureStorage().write(
|
|
key: oldKey,
|
|
value: 'old pending',
|
|
);
|
|
final account = FakeAccount(envelope)..fail = false;
|
|
await HostedBackupService(
|
|
account,
|
|
identity,
|
|
hpke: FakeHpke(envelope),
|
|
).backup();
|
|
expect(
|
|
await const FlutterSecureStorage().read(key: oldKey),
|
|
'old pending',
|
|
);
|
|
expect(account.enrollments, 1);
|
|
},
|
|
);
|
|
}
|