Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions test/support/memory_secrets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';

/// In-memory secure storage for wallet tests, with optional failure hooks.
class MemorySecrets extends Fake implements SecureStorageInterface {
MemorySecrets([Map<String, String>? initial])
: values = initial ?? {'xelis_wants_full_tables': 'false'};

final Map<String, String> values;
final List<String> writes = [];
final List<String> deletes = [];
String? failingKey;
String? failingDeleteKey;
Future<void> Function(String)? beforeDelete;

@override
dynamic noSuchMethod(Invocation invocation) {
final key = invocation.namedArguments[#key] as String;
switch (invocation.memberName) {
case #delete:
return () async {
await beforeDelete?.call(key);
if (key == failingDeleteKey) {
throw StateError('fixture delete failure');
}
deletes.add(key);
values.remove(key);
}();
case #read:
return Future<String?>.value(values[key]);
case #write:
if (key == failingKey) {
return Future<void>.error(StateError('fixture write failure'));
}
writes.add(key);
values[key] = invocation.namedArguments[#value] as String;
return Future<void>.value();
default:
return super.noSuchMethod(invocation);
}
}
}
45 changes: 45 additions & 0 deletions test/support/memory_secrets_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter_test/flutter_test.dart';

import 'memory_secrets.dart';

void main() {
test('reads, records writes, and records deletes', () async {
final secrets = MemorySecrets({'existing': 'value'});

expect(await secrets.read(key: 'existing'), 'value');
await secrets.write(key: 'new', value: 'secret');
await secrets.delete(key: 'existing');

expect(secrets.values, {'new': 'secret'});
expect(secrets.writes, ['new']);
expect(secrets.deletes, ['existing']);
});

test('runs the delete hook before injecting a failure', () async {
final order = <String>[];
final secrets = MemorySecrets({'blocked': 'value'})
..failingDeleteKey = 'blocked'
..beforeDelete = (key) async => order.add(key);

await expectLater(
secrets.delete(key: 'blocked'),
throwsA(isA<StateError>()),
);

expect(order, ['blocked']);
expect(secrets.values['blocked'], 'value');
expect(secrets.deletes, isEmpty);
});

test('a failed write does not change storage or its audit trail', () async {
final secrets = MemorySecrets()..failingKey = 'blocked';

await expectLater(
secrets.write(key: 'blocked', value: 'secret'),
throwsA(isA<StateError>()),
);

expect(secrets.values.containsKey('blocked'), isFalse);
expect(secrets.writes, isEmpty);
});
}
34 changes: 2 additions & 32 deletions test/wallets/support/xelis_test_fakes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/utilities/enums/sync_type_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/wallets/wallet/impl/xelis_wallet.dart';
import 'package:stackwallet/wallets/wallet/wallet.dart';
import 'package:stackwallet/wl_gen/interfaces/lib_xelis_interface.dart';

export '../../support/memory_secrets.dart';

class FakeNative extends Fake implements LibXelisInterface {
@override
String get xelisAsset => 'xel';
Expand Down Expand Up @@ -103,34 +104,3 @@ class ConfirmationPrefs extends ChangeNotifier implements Prefs {
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class MemorySecrets extends Fake implements SecureStorageInterface {
final values = <String, String>{'xelis_wants_full_tables': 'false'};
String? failingKey;
String? failingDeleteKey;
Future<void> Function(String)? beforeDelete;
@override
dynamic noSuchMethod(Invocation invocation) {
final key = invocation.namedArguments[#key] as String;
switch (invocation.memberName) {
case #delete:
return () async {
await beforeDelete?.call(key);
if (key == failingDeleteKey) {
throw StateError('fixture delete failure');
}
values.remove(key);
}();
case #read:
return Future<String?>.value(values[key]);
case #write:
if (key == failingKey) {
return Future<void>.error(StateError('fixture write failure'));
}
values[key] = invocation.namedArguments[#value] as String;
return Future<void>.value();
default:
return super.noSuchMethod(invocation);
}
}
}
Loading