109 lines
3.5 KiB
Dart
109 lines
3.5 KiB
Dart
import 'dart:convert';
|
|||
|
|
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:openchamber/api/pairing.dart';
|
||
|
|
|
||
|
|
String _link(Map<String, dynamic> payload) =>
|
||
|
|
'openchamber://connect?v=2&p=${base64Url.encode(utf8.encode(jsonEncode(payload))).replaceAll('=', '')}';
|
||
|
|
|
||
|
|
Map<String, dynamic> _payload({
|
||
|
|
String? expiresAt,
|
||
|
|
List<Map<String, dynamic>>? candidates,
|
||
|
|
}) =>
|
||
|
|
{
|
||
|
|
'v': 2,
|
||
|
|
'pairingId': 'pair_123',
|
||
|
|
'secret': 'secret-abc',
|
||
|
|
'label': '我的电脑',
|
||
|
|
if (expiresAt != null) 'expiresAt': expiresAt,
|
||
|
|
'candidates': candidates ??
|
||
|
|
[
|
||
|
|
{'type': 'lan', 'url': 'http://192.168.1.10:3000', 'priority': 10},
|
||
|
|
{
|
||
|
|
'type': 'relay',
|
||
|
|
'relayUrl': 'wss://relay.openchamber.dev/ws',
|
||
|
|
'serverId': 'server-1',
|
||
|
|
'hostEncPubJwk': {
|
||
|
|
'kty': 'EC',
|
||
|
|
'crv': 'P-256',
|
||
|
|
'x': 'abc',
|
||
|
|
'y': 'def',
|
||
|
|
},
|
||
|
|
'priority': 30,
|
||
|
|
},
|
||
|
|
],
|
||
|
|
};
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
test('parses a link with direct and relay candidates, direct first', () {
|
||
|
|
final payload = parsePairingLink(_link(_payload()));
|
||
|
|
expect(payload, isNotNull);
|
||
|
|
expect(payload!.pairingId, 'pair_123');
|
||
|
|
expect(payload.secret, 'secret-abc');
|
||
|
|
expect(payload.label, '我的电脑');
|
||
|
|
expect(payload.candidates, hasLength(2));
|
||
|
|
expect(payload.candidates.first.isRelay, isFalse);
|
||
|
|
expect(payload.candidates.first.url, 'http://192.168.1.10:3000');
|
||
|
|
|
||
|
|
final relay = payload.candidates.last;
|
||
|
|
expect(relay.isRelay, isTrue);
|
||
|
|
expect(relay.relayUrl, 'wss://relay.openchamber.dev/ws');
|
||
|
|
expect(relay.serverId, 'server-1');
|
||
|
|
expect(relay.hostKey?['x'], 'abc');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a relay-only link works and keeps the grant', () {
|
||
|
|
final payload = parsePairingLink(_link(_payload(candidates: [
|
||
|
|
{
|
||
|
|
'type': 'relay',
|
||
|
|
'relayUrl': 'wss://relay.example/ws',
|
||
|
|
'serverId': 'srv',
|
||
|
|
'hostEncPubJwk': {'kty': 'EC', 'crv': 'P-256', 'x': 'x', 'y': 'y'},
|
||
|
|
'grant': 'g-1',
|
||
|
|
},
|
||
|
|
])));
|
||
|
|
expect(payload, isNotNull);
|
||
|
|
expect(payload!.candidates.single.isRelay, isTrue);
|
||
|
|
expect(payload.candidates.single.grant, 'g-1');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a bare base64url payload is accepted too', () {
|
||
|
|
final encoded =
|
||
|
|
base64Url.encode(utf8.encode(jsonEncode(_payload()))).replaceAll('=', '');
|
||
|
|
expect(parsePairingLink(encoded), isNotNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('expired and malformed links are refused', () {
|
||
|
|
final past = DateTime.now()
|
||
|
|
.subtract(const Duration(minutes: 1))
|
||
|
|
.toUtc()
|
||
|
|
.toIso8601String();
|
||
|
|
expect(parsePairingLink(_link(_payload(expiresAt: past))), isNull);
|
||
|
|
expect(parsePairingLink('openchamber://connect?v=2&p=not-json'), isNull);
|
||
|
|
expect(parsePairingLink('https://example.com'), isNull);
|
||
|
|
|
||
|
|
// A candidate with a missing key or a bad scheme invalidates the list.
|
||
|
|
expect(
|
||
|
|
parsePairingLink(_link(_payload(candidates: [
|
||
|
|
{
|
||
|
|
'type': 'relay',
|
||
|
|
'relayUrl': 'https://relay.example/ws',
|
||
|
|
'serverId': 'srv',
|
||
|
|
'hostEncPubJwk': {'kty': 'EC', 'crv': 'P-256', 'x': 'x', 'y': 'y'},
|
||
|
|
},
|
||
|
|
]))),
|
||
|
|
isNull,
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
parsePairingLink(_link(_payload(candidates: [
|
||
|
|
{
|
||
|
|
'type': 'relay',
|
||
|
|
'relayUrl': 'wss://relay.example/ws',
|
||
|
|
'serverId': 'srv',
|
||
|
|
'hostEncPubJwk': {'kty': 'EC', 'crv': 'P-384', 'x': 'x', 'y': 'y'},
|
||
|
|
},
|
||
|
|
]))),
|
||
|
|
isNull,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
}
|