Improve VPN video routing, profile storage and authorization reuse
This commit is contained in:
@@ -1,9 +1,136 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshport_flutter/meshport_flutter.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'macOS session reuses authorized IPC after stop and cleans up on exit',
|
||||
() async {
|
||||
var launches = 0;
|
||||
final paths = <String>[];
|
||||
final sockets = <Socket>[];
|
||||
var state = 'Stopped';
|
||||
var idleChecks = 0;
|
||||
final session = MacSystemVpnSession(
|
||||
keepAlive: const Duration(milliseconds: 10),
|
||||
open: () => openMacSystemVpn(
|
||||
launch: (address, uid) async {
|
||||
launches++;
|
||||
paths.add(address);
|
||||
final socket = await Socket.connect(
|
||||
InternetAddress(address, type: InternetAddressType.unix),
|
||||
0,
|
||||
);
|
||||
sockets.add(socket);
|
||||
socket
|
||||
.cast<List<int>>()
|
||||
.transform(utf8.decoder)
|
||||
.transform(const LineSplitter())
|
||||
.listen((line) {
|
||||
final op = (jsonDecode(line) as Map<String, dynamic>)['op'];
|
||||
if (op == 'start') state = 'Running';
|
||||
if (op == 'stop') state = 'Stopped';
|
||||
if (op == 'status' && state == 'Stopped') idleChecks++;
|
||||
socket.write(
|
||||
'${jsonEncode({
|
||||
'ok': true,
|
||||
'data': {'state': state},
|
||||
})}\n',
|
||||
);
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
try {
|
||||
final first = await session.acquire();
|
||||
await first.request({'op': 'start'});
|
||||
await session.release(first);
|
||||
expect(state, 'Stopped');
|
||||
expect(await Directory(File(paths.single).parent.path).exists(), true);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
expect(idleChecks, greaterThan(0));
|
||||
final second = await session.acquire();
|
||||
expect(identical(first, second), true);
|
||||
expect(launches, 1);
|
||||
await second.request({'op': 'start'});
|
||||
await session.release(second);
|
||||
await session.close();
|
||||
expect(state, 'Stopped');
|
||||
expect(await Directory(File(paths.single).parent.path).exists(), false);
|
||||
await expectLater(session.acquire(), throwsStateError);
|
||||
} finally {
|
||||
await session.close();
|
||||
for (final socket in sockets) {
|
||||
socket.destroy();
|
||||
}
|
||||
}
|
||||
},
|
||||
skip: !Platform.isMacOS,
|
||||
);
|
||||
|
||||
test(
|
||||
'dead helper is replaced and cancelled authorization can be retried',
|
||||
() async {
|
||||
var opens = 0, closed = 0;
|
||||
var failed = false;
|
||||
final session = MacSystemVpnSession(
|
||||
open: () async {
|
||||
opens++;
|
||||
if (opens == 1) throw StateError('authorization cancelled');
|
||||
return MeshPortClient.fromTransport(
|
||||
request: (request) async {
|
||||
if (failed && opens == 2) throw StateError('helper disconnected');
|
||||
return {
|
||||
'ok': true,
|
||||
'data': {'state': 'Stopped'},
|
||||
};
|
||||
},
|
||||
close: () async {
|
||||
closed++;
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
try {
|
||||
await expectLater(session.acquire(), throwsStateError);
|
||||
final first = await session.acquire();
|
||||
await session.release(first);
|
||||
failed = true;
|
||||
final replacement = await session.acquire();
|
||||
expect(identical(first, replacement), false);
|
||||
expect(opens, 3);
|
||||
expect(closed, 1);
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
expect(closed, 2);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'closing while authorization is pending does not leave a helper',
|
||||
() async {
|
||||
final pending = Completer<MeshPortClient>();
|
||||
var closes = 0;
|
||||
final session = MacSystemVpnSession(open: () => pending.future);
|
||||
final opening = session.acquire();
|
||||
final rejected = expectLater(opening, throwsStateError);
|
||||
final closing = session.close();
|
||||
pending.complete(
|
||||
MeshPortClient.fromTransport(
|
||||
request: (_) async => {'ok': true, 'data': {}},
|
||||
close: () async {
|
||||
closes++;
|
||||
},
|
||||
),
|
||||
);
|
||||
await rejected;
|
||||
await closing;
|
||||
expect(closes, 1);
|
||||
},
|
||||
);
|
||||
test(
|
||||
'Unix control serializes replies and removes socket on close',
|
||||
() async {
|
||||
|
||||
Reference in New Issue
Block a user