Files
MeshPort/apps/meshport_lab/test/proxy_session_test.dart
T
shenleiandClaude Haiku 5.5 2f185f914d Snapshot uncommitted work: proxy UI, sing-box integration, socks bridge
- Lab app: proxy panel, node editor, configuration and detail pages,
  responsive layout, and related tests
- Add meshport-socks crate and sing-box bridge scripts/docs
- Add singbox_flutter binding and meshport_client_core package
- FFI, VPN helper (unified mac path), and Android/macOS runner updates
- Docs for architecture, client runtime, UI design, and validation
- UI design output artifacts under outputs/ui

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-12 10:06:15 +09:00

297 lines
9.4 KiB
Dart

import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_flutter/meshport_flutter.dart';
import 'package:meshport_client_core/meshport_client_core.dart';
import 'package:singbox_flutter/singbox_flutter.dart';
class FakeProxy implements ProxyEngine {
final List<String> events;
String state = 'Stopped';
bool failStart = false;
Map<String, dynamic>? lastProfile;
FakeProxy(this.events);
@override
Future<Map<String, dynamic>> start({
required int port,
required String meshSocks,
required List<String> prefixes,
Map<String, dynamic>? profile,
}) async {
lastProfile = profile;
events.add('proxy.start:${prefixes.join(',')}');
if (failStart) throw StateError('occupied port');
state = 'Running';
return {'state': state};
}
@override
Future<Map<String, dynamic>> status() async => {'state': state};
@override
Future<void> stop() async {
events.add('proxy.stop');
state = 'Stopped';
}
@override
Future<void> close() async {
events.add('proxy.close');
}
}
class Fixture {
final events = <String>[];
late final proxy = FakeProxy(events);
late final mesh = MeshPortClient.fromTransport(
request: request,
close: () async {},
);
late final session = MeshProxySession(mesh: mesh, proxy: proxy);
String state = 'Stopped', mode = 'application';
String? ip = '10.77.0.11/24';
List<String> routes = ['192.168.21.0/24'];
Future<Map<String, dynamic>> request(Map<String, dynamic> input) async {
final op = input['op'] as String;
events.add('mesh.$op');
final data = switch (op) {
'validate' => {'runtime_mode': mode},
'start' => (() {
state = 'Running';
return <String, dynamic>{};
})(),
'stop' => (() {
state = 'Stopped';
return <String, dynamic>{};
})(),
'socks_start' => {'listen': '127.0.0.1:50001'},
'socks_status' => {'running': true},
'status' => {
'state': state,
'ipv4': ip,
'platform_network': {
'subnet_routes_scope': 'accepted_projection',
'subnet_routes': routes,
},
},
_ => <String, dynamic>{},
};
return {'ok': true, 'data': data};
}
}
void main() {
test(
'joint lifecycle, canonical routes, update and ordered cleanup',
() async {
final f = Fixture();
await f.session.start('test');
expect(f.session.state, 'Running');
expect(f.session.endpoint, '127.0.0.1:10818');
final starts = f.events.where((e) => e.startsWith('proxy.start')).length;
f.routes = ['192.168.21.17/24', '192.168.21.0/24'];
await f.session.refresh();
expect(f.events.where((e) => e.startsWith('proxy.start')).length, starts);
f.routes = ['192.168.22.0/24'];
await f.session.refresh();
expect(f.events.last, 'proxy.start:10.77.0.0/24,192.168.22.0/24');
expect(f.proxy.lastProfile?['protected_cidrs'], [
'10.77.0.0/24',
'192.168.21.0/24',
'192.168.22.0/24',
]);
f.events.clear();
await f.session.stop();
expect(f.events, ['proxy.stop', 'mesh.socks_stop', 'mesh.stop']);
await f.session.start('test');
await f.session.close();
expect(f.events.last, 'proxy.close');
await expectLater(f.session.start('test'), throwsStateError);
},
);
test('failed engine startup rolls back and permits retry', () async {
final f = Fixture();
f.proxy.failStart = true;
await expectLater(f.session.start('test'), throwsStateError);
expect(f.session.state, 'Failed');
expect(f.state, 'Stopped');
expect(f.events.sublist(f.events.length - 3), [
'proxy.stop',
'mesh.socks_stop',
'mesh.stop',
]);
f.proxy.failStart = false;
await f.session.start('test');
await f.session.close();
});
test(
'queue prevents concurrent duplicate starts; system mode is refused',
() async {
final f = Fixture();
final first = f.session.start('test');
final duplicate = f.session.start('test');
await expectLater(duplicate, throwsStateError);
await first;
expect(f.events.where((e) => e == 'mesh.start').length, 1);
await f.session.stop();
f.mode = 'system_vpn';
await expectLater(f.session.start('test'), throwsUnsupportedError);
await f.session.close();
},
);
test(
'wait for DHCP then start; unexpected engine failure cleans mesh',
() async {
final f = Fixture();
f.ip = null;
f.routes = [];
await f.session.start('test');
expect(f.session.state, 'AwaitingAddress');
f.ip = '10.77.0.11/24';
await f.session.refresh();
expect(f.session.state, 'Running');
f.proxy.state = 'Failed';
await expectLater(f.session.refresh(), throwsStateError);
expect(f.session.state, 'Failed');
expect(f.state, 'Stopped');
await f.session.close();
},
);
test(
'reject public/overbroad projection and ignore untrusted route scope',
() {
expect(
() => effectiveMeshPrefixes({'ipv4': '8.8.8.8/24'}),
throwsStateError,
);
expect(
() => effectiveMeshPrefixes({'ipv4': '10.0.0.1/8'}),
throwsStateError,
);
expect(
effectiveMeshPrefixes({
'ipv4': '10.77.0.11/24',
'platform_network': {
'subnet_routes': ['0.0.0.0/0'],
},
}),
['10.77.0.0/24'],
);
},
);
test(
'proxy-only session does not start mesh or require network credentials',
() async {
final f = Fixture();
final session = MeshProxySession(
mesh: f.mesh,
proxy: f.proxy,
profile: {'feature': 'proxy', 'mode': 'direct'},
);
await session.start('invalid mesh config');
expect(session.state, 'Running');
expect(
f.events.where((e) => e == 'mesh.start' || e == 'mesh.validate'),
isEmpty,
);
expect(f.proxy.lastProfile?['feature'], 'proxy');
await session.close();
},
);
test(
'combined keeps proxy available and protects mesh when native network stops',
() async {
final f = Fixture();
final session = MeshProxySession(
mesh: f.mesh,
proxy: f.proxy,
profile: {'feature': 'combined', 'mode': 'global'},
);
await session.start('test');
f.state = 'Stopped';
await session.refresh();
expect(session.state, 'Partial');
expect(session.endpoint, isNotNull);
expect(f.proxy.lastProfile?['protected_cidrs'], [
'10.77.0.0/24',
'192.168.21.0/24',
]);
await session.close();
},
);
final path = Platform.environment['MESHPORT_PROXY_LIBRARY'];
test(
'native Go ABI loads from Dart, releases listeners and closes context',
() async {
final listener = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
final port = listener.port;
await listener.close();
final engine = await NativeProxyEngine.open(libraryPath: path);
expect((await engine.status())['state'], 'Stopped');
await engine.start(
port: port,
meshSocks: '127.0.0.1:9',
prefixes: ['10.77.0.0/24'],
);
expect((await engine.status())['state'], 'Running');
final socket = await Socket.connect('127.0.0.1', port);
await engine.close();
expect(
await socket
.fold<int>(0, (count, bytes) => count + bytes.length)
.timeout(const Duration(seconds: 2)),
0,
);
socket.destroy();
final reused = await ServerSocket.bind(
InternetAddress.loopbackIPv4,
port,
);
await reused.close();
await expectLater(engine.status(), throwsStateError);
},
skip: path == null
? 'Set MESHPORT_PROXY_LIBRARY after building native library'
: false,
);
final meshPath = Platform.environment['MESHPORT_LIBRARY'];
test(
'Rust and Go coexist in one Dart process with joint native cleanup',
() async {
final mesh = await MeshPortClient.open(libraryPath: meshPath);
final engine = await NativeProxyEngine.open(libraryPath: path);
final reserve = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
final port = reserve.port;
await reserve.close();
final session = MeshProxySession(mesh: mesh, proxy: engine, port: port);
try {
await session.start(
"ipv4 = '10.253.82.11/24'\nstun_servers = []\nstun_servers_v6 = []\nlisteners = []\n[network_identity]\nnetwork_name = 'native-lifecycle-test'\nnetwork_secret = 'test-only'\n[flags]\nno_tun = true\nuse_smoltcp = true\n",
);
expect(session.state, 'Running');
expect((await mesh.socksBridgeStatus())['running'], true);
final socket = await Socket.connect('127.0.0.1', port);
await session.stop();
expect(
await socket
.fold<int>(0, (n, b) => n + b.length)
.timeout(const Duration(seconds: 2)),
0,
);
socket.destroy();
expect((await mesh.status())['state'], 'Stopped');
expect((await mesh.socksBridgeStatus())['running'], false);
final reuse = await ServerSocket.bind(
InternetAddress.loopbackIPv4,
port,
);
await reuse.close();
} finally {
await session.close();
await mesh.close();
}
},
skip: path == null || meshPath == null
? 'Set both native library paths'
: false,
);
}