[fuchsia_remote_debug_protocol] Adds the fuchsia remote debug protocol package.
Adds a package that will support connecting to and debugging devices with multiple instances of the Dart VM, and operate on multiple Isolates and Flutter Views. Currently supports connecting to all existing Dart VMs and extracting information about all Flutter Views running across each VM.
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
// Copyright 2018 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
|
||||
|
||||
import 'package:fuchsia_remote_debug_protocol/fuchsia_remote_debug_protocol.dart';
|
||||
|
||||
void main() {
|
||||
group('FuchsiaRemoteConnection.connect', () {
|
||||
MockSshCommandRunner mockRunner;
|
||||
|
||||
setUp(() {
|
||||
mockRunner = new MockSshCommandRunner();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
/// Most tests will mock out the port forwarding and connection
|
||||
/// functions.
|
||||
restoreFuchsiaPortForwardingFunction();
|
||||
restoreVmServiceConnectionFunction();
|
||||
});
|
||||
|
||||
test('end-to-end with three vm connections and flutter view query',
|
||||
() async {
|
||||
const String address = 'fe80::8eae:4cff:fef4:9247';
|
||||
const String interface = 'eno1';
|
||||
// Adds some extra junk to make sure the strings will be cleaned up.
|
||||
when(mockRunner.run(typed(any)))
|
||||
.thenReturn(<String>['123\n\n\n', '456 ', '789']);
|
||||
when(mockRunner.address).thenReturn(address);
|
||||
when(mockRunner.interface).thenReturn(interface);
|
||||
int port = 0;
|
||||
final List<MockPortForwarder> forwardedPorts = <MockPortForwarder>[];
|
||||
Future<PortForwarder> mockPortForwardingFunction(
|
||||
String address, int remotePort,
|
||||
[String interface = '', String configFile]) {
|
||||
return new Future<PortForwarder>(() {
|
||||
final MockPortForwarder pf = new MockPortForwarder();
|
||||
forwardedPorts.add(pf);
|
||||
when(pf.port).thenReturn(port++);
|
||||
when(pf.remotePort).thenReturn(remotePort);
|
||||
return pf;
|
||||
});
|
||||
}
|
||||
|
||||
int flutterViewIndex = 0;
|
||||
final List<Map<String, dynamic>> flutterViewCannedResponses =
|
||||
<Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView0',
|
||||
},
|
||||
],
|
||||
},
|
||||
<String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView1',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/1',
|
||||
'name': 'file://flutterBinary1',
|
||||
'number': '1',
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
<String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView2',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/2',
|
||||
'name': 'file://flutterBinary2',
|
||||
'number': '2',
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
final List<MockPeer> mockPeerConnections = <MockPeer>[];
|
||||
final List<Uri> uriConnections = <Uri>[];
|
||||
Future<json_rpc.Peer> mockVmConnectionFunction(Uri uri) {
|
||||
return new Future<json_rpc.Peer>(() async {
|
||||
final MockPeer mp = new MockPeer();
|
||||
mockPeerConnections.add(mp);
|
||||
uriConnections.add(uri);
|
||||
when(mp.sendRequest(typed<String>(any), typed<String>(any)))
|
||||
.thenReturn(new Future<Map<String, dynamic>>(
|
||||
() => flutterViewCannedResponses[flutterViewIndex++]));
|
||||
return mp;
|
||||
});
|
||||
}
|
||||
|
||||
fuchsiaPortForwardingFunction = mockPortForwardingFunction;
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
|
||||
final FuchsiaRemoteConnection connection =
|
||||
await FuchsiaRemoteConnection.connectWithSshCommandRunner(mockRunner);
|
||||
|
||||
// [mockPortForwardingFunction] will have returned three different
|
||||
// forwarded ports, incrementing the port each time by one. (Just a sanity
|
||||
// check that the forwarding port was called).
|
||||
expect(forwardedPorts.length, 3);
|
||||
expect(forwardedPorts[0].remotePort, 123);
|
||||
expect(forwardedPorts[1].remotePort, 456);
|
||||
expect(forwardedPorts[2].remotePort, 789);
|
||||
expect(forwardedPorts[0].port, 0);
|
||||
expect(forwardedPorts[1].port, 1);
|
||||
expect(forwardedPorts[2].port, 2);
|
||||
|
||||
final List<FlutterView> views = await connection.getFlutterViews();
|
||||
expect(views, isNot(null));
|
||||
expect(views.length, 3);
|
||||
// Since name can be null, check for the ID on all of them.
|
||||
expect(views[0].id, 'flutterView0');
|
||||
expect(views[1].id, 'flutterView1');
|
||||
expect(views[2].id, 'flutterView2');
|
||||
|
||||
expect(views[0].name, equals(null));
|
||||
expect(views[1].name, 'file://flutterBinary1');
|
||||
expect(views[2].name, 'file://flutterBinary2');
|
||||
|
||||
// Ensure the ports are all closed after stop was called.
|
||||
await connection.stop();
|
||||
verify(forwardedPorts[0].stop());
|
||||
verify(forwardedPorts[1].stop());
|
||||
verify(forwardedPorts[2].stop());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockSshCommandRunner extends Mock implements SshCommandRunner {}
|
||||
|
||||
class MockPortForwarder extends Mock implements PortForwarder {}
|
||||
|
||||
class MockPeer extends Mock implements json_rpc.Peer {}
|
||||
@@ -0,0 +1,228 @@
|
||||
// Copyright 2018 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:fuchsia_remote_debug_protocol/src/dart/dart_vm.dart';
|
||||
import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('DartVm.connect', () {
|
||||
tearDown(() {
|
||||
restoreVmServiceConnectionFunction();
|
||||
});
|
||||
|
||||
test('null connector', () async {
|
||||
Future<json_rpc.Peer> mockServiceFunction(Uri uri) {
|
||||
return new Future<json_rpc.Peer>(() => null);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockServiceFunction;
|
||||
expect(await DartVm.connect(Uri.parse('http://this.whatever/ws')),
|
||||
equals(null));
|
||||
});
|
||||
|
||||
test('disconnect closes peer', () async {
|
||||
final MockPeer peer = new MockPeer();
|
||||
Future<json_rpc.Peer> mockServiceFunction(Uri uri) {
|
||||
return new Future<json_rpc.Peer>(() => peer);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockServiceFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://this.whatever/ws'));
|
||||
expect(vm, isNot(null));
|
||||
await vm.stop();
|
||||
verify(peer.close());
|
||||
});
|
||||
});
|
||||
|
||||
group('DartVm.getAllFlutterViews', () {
|
||||
MockPeer mockPeer;
|
||||
|
||||
setUp(() {
|
||||
mockPeer = new MockPeer();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
restoreVmServiceConnectionFunction();
|
||||
});
|
||||
|
||||
test('basic flutter view parsing', () async {
|
||||
final Map<String, dynamic> flutterViewCannedResponses = <String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView0',
|
||||
},
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView1',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/1',
|
||||
'name': 'file://flutterBinary1',
|
||||
'number': '1',
|
||||
},
|
||||
},
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView2',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/2',
|
||||
'name': 'file://flutterBinary2',
|
||||
'number': '2',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
Future<json_rpc.Peer> mockVmConnectionFunction(Uri uri) {
|
||||
when(mockPeer.sendRequest(
|
||||
typed<String>(any), typed<Map<String, dynamic>>(any)))
|
||||
.thenReturn(new Future<Map<String, dynamic>>(
|
||||
() => flutterViewCannedResponses));
|
||||
return new Future<json_rpc.Peer>(() => mockPeer);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
final List<FlutterView> views = await vm.getAllFlutterViews();
|
||||
expect(views.length, 3);
|
||||
// Check ID's as they cannot be null.
|
||||
expect(views[0].id, 'flutterView0');
|
||||
expect(views[1].id, 'flutterView1');
|
||||
expect(views[2].id, 'flutterView2');
|
||||
|
||||
// Verify names.
|
||||
expect(views[0].name, equals(null));
|
||||
expect(views[1].name, 'file://flutterBinary1');
|
||||
expect(views[2].name, 'file://flutterBinary2');
|
||||
});
|
||||
|
||||
test('invalid flutter view missing ID', () async {
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingId =
|
||||
<String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
// Valid flutter view.
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView1',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'name': 'IsolateThing',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/1',
|
||||
'number': '1',
|
||||
},
|
||||
},
|
||||
|
||||
// Missing ID.
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
},
|
||||
]
|
||||
};
|
||||
|
||||
Future<json_rpc.Peer> mockVmConnectionFunction(Uri uri) {
|
||||
when(mockPeer.sendRequest(
|
||||
typed<String>(any), typed<Map<String, dynamic>>(any)))
|
||||
.thenReturn(new Future<Map<String, dynamic>>(
|
||||
() => flutterViewCannedResponseMissingId));
|
||||
return new Future<json_rpc.Peer>(() => mockPeer);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
Future<Null> failingFunction() async {
|
||||
await vm.getAllFlutterViews();
|
||||
}
|
||||
|
||||
// Both views should be invalid as they were missing required fields.
|
||||
expect(failingFunction, throwsA(const isInstanceOf<RpcFormatError>()));
|
||||
});
|
||||
|
||||
test('invalid flutter view missing ID', () async {
|
||||
final Map<String, dynamic> flutterViewCannedResponseMissingIsolateName =
|
||||
<String, dynamic>{
|
||||
'views': <Map<String, dynamic>>[
|
||||
// Missing isolate name.
|
||||
<String, dynamic>{
|
||||
'type': 'FlutterView',
|
||||
'id': 'flutterView1',
|
||||
'isolate': <String, dynamic>{
|
||||
'type': '@Isolate',
|
||||
'fixedId': 'true',
|
||||
'id': 'isolates/1',
|
||||
'number': '1',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
Future<json_rpc.Peer> mockVmConnectionFunction(Uri uri) {
|
||||
when(mockPeer.sendRequest(
|
||||
typed<String>(any), typed<Map<String, dynamic>>(any)))
|
||||
.thenReturn(new Future<Map<String, dynamic>>(
|
||||
() => flutterViewCannedResponseMissingIsolateName));
|
||||
return new Future<json_rpc.Peer>(() => mockPeer);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
Future<Null> failingFunction() async {
|
||||
await vm.getAllFlutterViews();
|
||||
}
|
||||
|
||||
// Both views should be invalid as they were missing required fields.
|
||||
expect(failingFunction, throwsA(const isInstanceOf<RpcFormatError>()));
|
||||
});
|
||||
});
|
||||
|
||||
group('DartVm.invokeRpc', () {
|
||||
MockPeer mockPeer;
|
||||
|
||||
setUp(() {
|
||||
mockPeer = new MockPeer();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
restoreVmServiceConnectionFunction();
|
||||
});
|
||||
|
||||
test('verify timeout fires', () async {
|
||||
const Duration timeoutTime = const Duration(milliseconds: 100);
|
||||
Future<json_rpc.Peer> mockVmConnectionFunction(Uri uri) {
|
||||
// Return a command that will never complete.
|
||||
when(mockPeer.sendRequest(
|
||||
typed<String>(any), typed<Map<String, dynamic>>(any)))
|
||||
.thenReturn(new Completer<Map<String, dynamic>>().future);
|
||||
return new Future<json_rpc.Peer>(() => mockPeer);
|
||||
}
|
||||
|
||||
fuchsiaVmServiceConnectionFunction = mockVmConnectionFunction;
|
||||
final DartVm vm =
|
||||
await DartVm.connect(Uri.parse('http://whatever.com/ws'));
|
||||
expect(vm, isNot(null));
|
||||
Future<Null> failingFunction() async {
|
||||
await vm.invokeRpc('somesillyfunction', timeout: timeoutTime);
|
||||
}
|
||||
|
||||
expect(failingFunction, throwsA(const isInstanceOf<TimeoutException>()));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockPeer extends Mock implements json_rpc.Peer {}
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright 2018 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' show ProcessResult;
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:fuchsia_remote_debug_protocol/src/runners/ssh_command_runner.dart';
|
||||
|
||||
void main() {
|
||||
group('SshCommandRunner.constructors', () {
|
||||
test('throws exception with invalid address', () async {
|
||||
SshCommandRunner newCommandRunner() {
|
||||
return new SshCommandRunner(address: 'sillyaddress.what');
|
||||
}
|
||||
|
||||
expect(newCommandRunner, throwsArgumentError);
|
||||
});
|
||||
|
||||
test('throws exception from injection constructor with invalid addr',
|
||||
() async {
|
||||
SshCommandRunner newCommandRunner() {
|
||||
return new SshCommandRunner.withProcessManager(
|
||||
const LocalProcessManager(),
|
||||
address: '192.168.1.1.1');
|
||||
}
|
||||
|
||||
expect(newCommandRunner, throwsArgumentError);
|
||||
});
|
||||
});
|
||||
|
||||
group('SshCommandRunner.run', () {
|
||||
MockProcessManager mockProcessManager;
|
||||
MockProcessResult mockProcessResult;
|
||||
SshCommandRunner runner;
|
||||
|
||||
setUp(() {
|
||||
mockProcessManager = new MockProcessManager();
|
||||
mockProcessResult = new MockProcessResult();
|
||||
when(mockProcessManager.run(typed(any))).thenReturn(mockProcessResult);
|
||||
});
|
||||
|
||||
test('verify interface is appended to ipv6 address', () async {
|
||||
const String ipV6Addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
const String interface = 'eno1';
|
||||
runner = new SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
address: ipV6Addr,
|
||||
interface: interface,
|
||||
sshConfigPath: '/whatever',
|
||||
);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(typed(captureAny))).captured.single;
|
||||
expect(passedCommand, contains('$ipV6Addr%$interface'));
|
||||
});
|
||||
|
||||
test('verify no percentage symbol is added when no ipv6 interface',
|
||||
() async {
|
||||
const String ipV6Addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
runner = new SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
address: ipV6Addr,
|
||||
);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(typed(captureAny))).captured.single;
|
||||
expect(passedCommand, contains(ipV6Addr));
|
||||
});
|
||||
|
||||
test('verify commands are split into multiple lines', () async {
|
||||
const String addr = '192.168.1.1';
|
||||
runner = new SshCommandRunner.withProcessManager(mockProcessManager,
|
||||
address: addr);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('''this
|
||||
has
|
||||
four
|
||||
lines''');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
final List<String> result = await runner.run('oihaw');
|
||||
expect(result, hasLength(4));
|
||||
});
|
||||
|
||||
test('verify exception on nonzero process result exit code', () async {
|
||||
const String addr = '192.168.1.1';
|
||||
runner = new SshCommandRunner.withProcessManager(mockProcessManager,
|
||||
address: addr);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('whatever');
|
||||
when(mockProcessResult.exitCode).thenReturn(1);
|
||||
Future<Null> failingFunction() async {
|
||||
await runner.run('oihaw');
|
||||
}
|
||||
|
||||
expect(failingFunction, throwsA(const isInstanceOf<SshCommandError>()));
|
||||
});
|
||||
|
||||
test('verify correct args with config', () async {
|
||||
const String addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
const String config = '/this/that/this/and/uh';
|
||||
runner = new SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
address: addr,
|
||||
sshConfigPath: config,
|
||||
);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(typed(captureAny))).captured.single;
|
||||
expect(passedCommand, contains('-F'));
|
||||
final int indexOfFlag = passedCommand.indexOf('-F');
|
||||
final String passedConfig = passedCommand[indexOfFlag + 1];
|
||||
expect(passedConfig, config);
|
||||
});
|
||||
|
||||
test('verify config is excluded correctly', () async {
|
||||
const String addr = 'fe80::8eae:4cff:fef4:9247';
|
||||
runner = new SshCommandRunner.withProcessManager(
|
||||
mockProcessManager,
|
||||
address: addr,
|
||||
);
|
||||
when<String>(mockProcessResult.stdout).thenReturn('somestuff');
|
||||
when(mockProcessResult.exitCode).thenReturn(0);
|
||||
await runner.run('ls /whatever');
|
||||
final List<String> passedCommand =
|
||||
verify(mockProcessManager.run(typed(captureAny))).captured.single;
|
||||
final int indexOfFlag = passedCommand.indexOf('-F');
|
||||
expect(indexOfFlag, equals(-1));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
|
||||
class MockProcessResult extends Mock implements ProcessResult {}
|
||||
Reference in New Issue
Block a user