import 'dart:async'; import 'dart:ui' as ui; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/clipboard_failures.dart'; import 'package:pipelines_app/application/insecure_clipboard.dart'; /// A browser page whose clipboard [unavailable] says whether it has the /// Clipboard API; [copied] records legacy copies, [paste] answers reads. class _Host implements InsecureClipboardHost { _Host({this.unavailable = true, this.copies = true}); @override final bool unavailable; final bool copies; final List copied = []; final List waits = []; Completer paste = Completer(); @override bool copy(String text) { copied.add(text); return copies; } @override Future nextPaste(Duration timeout) { waits.add(timeout); return paste.future; } } /// The engine: records what reaches it and answers every call successfully; /// keeps the handlers set on it. class _Engine implements BinaryMessenger { final List methods = []; final Map handlers = {}; static const codec = JSONMethodCodec(); @override Future? send(String channel, ByteData? message) async { methods.add(codec.decodeMethodCall(message).method); return codec.encodeSuccessEnvelope({'text': 'from the engine'}); } @override Future handlePlatformMessage( String channel, ByteData? data, ui.PlatformMessageResponseCallback? callback, ) async {} @override void setMessageHandler(String channel, MessageHandler? handler) { if (handler == null) { handlers.remove(channel); } else { handlers[channel] = handler; } } } void main() { const keys = JSONMessageCodec(); late _Host host; late _Engine engine; late List reports; late MethodChannel platform; Future? pasted; void setUpPage({bool unavailable = true, bool copies = true}) { host = _Host(unavailable: unavailable, copies: copies); engine = _Engine(); reports = []; final messenger = ClipboardReportingMessenger( engine, report: reports.add, insecure: InsecureClipboard(host), ); platform = MethodChannel( SystemChannels.platform.name, const JSONMethodCodec(), messenger, ); // The framework's key handler: its handling of a key event may read the // clipboard (the terminal's paste key) and it reports the key handled. messenger.setMessageHandler(SystemChannels.keyEvent.name, (message) async { final read = keys.decodeMessage(message) == 'paste'; if (read) { pasted = platform.invokeMethod( 'Clipboard.getData', Clipboard.kTextPlain, ); } return keys.encodeMessage({'handled': true}); }); } Future pressKey(String what) async { final reply = await engine.handlers[SystemChannels.keyEvent.name]!( keys.encodeMessage(what), ); return keys.decodeMessage(reply); } setUp(() { pasted = null; setUpPage(); }); test('a copy on an insecure page uses the legacy copy, not the engine', () async { await platform.invokeMethod('Clipboard.setData', {'text': 'hello'}); expect(host.copied, ['hello']); expect(engine.methods, isEmpty); expect(reports, isEmpty); }); test('a refused legacy copy says the page is not secure', () async { setUpPage(copies: false); await expectLater( platform.invokeMethod('Clipboard.setData', {'text': 'hello'}), throwsA( isA().having( (e) => e.code, 'code', insecureClipboardCode, ), ), ); expect(reports, [insecureCopyMessage()]); expect(insecureCopyMessage(), contains("isn't secure (http)")); }); test('a secure page keeps the engine clipboard', () async { setUpPage(unavailable: false); await platform.invokeMethod('Clipboard.setData', {'text': 'hello'}); final data = await platform.invokeMethod( 'Clipboard.getData', Clipboard.kTextPlain, ); expect(host.copied, isEmpty); expect(engine.methods, ['Clipboard.setData', 'Clipboard.getData']); expect(data, {'text': 'from the engine'}); }); test('a read outside a key event (a Paste button) says to use the paste ' 'key', () async { await expectLater( platform.invokeMethod('Clipboard.getData', Clipboard.kTextPlain), throwsA(isA()), ); expect(host.waits, isEmpty); expect(reports, [insecurePasteMessage()]); expect(insecurePasteMessage(), contains('Ctrl+V')); }); group('a paste key', () { test('reads its browser paste event and lets the browser paste', () async { final reply = pressKey('paste'); // The read was asked for during the key event; it waits for the paste. expect(host.waits, hasLength(1)); expect(await reply, {'handled': false}); host.paste.complete('pasted text'); expect(await pasted, {'text': 'pasted text'}); expect(engine.methods, isEmpty); expect(reports, isEmpty); }); test('other keys keep their handled reply', () async { expect(await pressKey('other'), {'handled': true}); expect(host.waits, isEmpty); }); test('a secure page leaves the key reply alone', () async { setUpPage(unavailable: false); expect(await pressKey('paste'), {'handled': true}); expect(await pasted, {'text': 'from the engine'}); }); }); test('a read during a key event yields the paste event text', () async { final clipboard = InsecureClipboard(host); Future? read; final handler = clipboard.keyEventHandler((message) { read = clipboard.answer( const MethodCall('Clipboard.getData', Clipboard.kTextPlain), ); return Future.value(keys.encodeMessage({'handled': true})); }); final reply = await handler(keys.encodeMessage('key')); expect(keys.decodeMessage(reply), {'handled': false}); host.paste.complete('pasted'); final data = const JSONMethodCodec().decodeEnvelope((await read)!); expect(data, {'text': 'pasted'}); }); test('a paste key with no paste event says why', () async { final clipboard = InsecureClipboard(host); Future? read; await clipboard.keyEventHandler((message) { read = clipboard.answer( const MethodCall('Clipboard.getData', Clipboard.kTextPlain), ); return Future.value(keys.encodeMessage({'handled': true})); })(keys.encodeMessage('key')); host.paste.complete(null); expect( () async => const JSONMethodCodec().decodeEnvelope((await read)!), throwsA( isA().having( (e) => e.message, 'message', insecurePasteMessage(), ), ), ); }); test('other formats and methods are left to the engine', () { final clipboard = InsecureClipboard(host); expect( clipboard.answer(const MethodCall('Clipboard.getData', 'text/html')), isNull, ); expect(clipboard.answer(const MethodCall('Clipboard.hasStrings')), isNull); }); }