You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.8 KiB
111 lines
3.8 KiB
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:meshcore_open/models/channel.dart';
|
|
import 'package:meshcore_open/services/link_router.dart';
|
|
|
|
// #1289: every share link goes through LinkRouter, which holds it until its
|
|
// confirmation can be shown and never adds anything itself.
|
|
|
|
const _key = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
|
|
const _channelLink =
|
|
'meshcore://channel/add?name=%23echo&secret=5d25cf40b1f5b4a7eb0cf9703634a948';
|
|
const _hashtagLink = 'meshcore://channel/add?name=%23example';
|
|
const _contactLink = 'meshcore://contact/add?name=Bob&public_key=$_key&type=1';
|
|
final _advertLink = 'meshcore://${'ab' * 98}';
|
|
|
|
void main() {
|
|
group('parseShareLink', () {
|
|
test('reads every link form Offband produces or accepts', () {
|
|
expect(parseShareLink(_channelLink), isA<ChannelShareLink>());
|
|
final hashtag = parseShareLink(_hashtagLink) as ChannelShareLink;
|
|
expect(
|
|
hashtag.channel.pskHex,
|
|
Channel(
|
|
index: 0,
|
|
name: '#example',
|
|
psk: Channel.derivePskFromHashtag('example'),
|
|
).pskHex,
|
|
);
|
|
final contact = parseShareLink(_contactLink) as ContactShareLink;
|
|
expect(contact.contact.name, 'Bob');
|
|
expect(contact.uri, _contactLink);
|
|
final advert = parseShareLink(_advertLink) as AdvertShareLink;
|
|
expect(advert.advert.length, 98);
|
|
});
|
|
|
|
test('tolerates surrounding whitespace', () {
|
|
expect(parseShareLink(' $_channelLink\n'), isA<ChannelShareLink>());
|
|
});
|
|
|
|
test('rejects anything else', () {
|
|
for (final bad in [
|
|
'https://example.com',
|
|
'meshcore://channel/add?name=x',
|
|
'meshcore://contact/add?name=Bob',
|
|
'meshcore://${'ab' * 10}',
|
|
'meshcore://${'ab' * 98}a',
|
|
'meshcore://zz',
|
|
'',
|
|
]) {
|
|
expect(parseShareLink(bad), isNull, reason: bad);
|
|
}
|
|
});
|
|
});
|
|
|
|
group('LinkRouter', () {
|
|
test('a malformed link is reported, not dropped', () {
|
|
final router = LinkRouter();
|
|
var notified = 0;
|
|
router.addListener(() => notified++);
|
|
expect(router.handle('meshcore://nope'), isFalse);
|
|
expect(notified, 1);
|
|
expect(router.pending, isNull);
|
|
expect(router.takeRejected(), 'meshcore://nope');
|
|
expect(router.takeRejected(), isNull);
|
|
});
|
|
|
|
test('a link is held while no radio is connected', () {
|
|
final router = LinkRouter()..handle(_channelLink);
|
|
expect(
|
|
router.takeIfReady(connected: false, channelTableBusy: false),
|
|
isNull,
|
|
);
|
|
expect(router.pending, isA<ChannelShareLink>());
|
|
final link = router.takeIfReady(connected: true, channelTableBusy: false);
|
|
expect(link, isA<ChannelShareLink>());
|
|
expect(router.pending, isNull);
|
|
});
|
|
|
|
test('a channel link waits for the channel table to settle', () {
|
|
final router = LinkRouter()..handle(_channelLink);
|
|
expect(
|
|
router.takeIfReady(connected: true, channelTableBusy: true),
|
|
isNull,
|
|
);
|
|
expect(router.pending, isNotNull);
|
|
});
|
|
|
|
test('a contact link does not wait for the channel table', () {
|
|
final router = LinkRouter()..handle(_contactLink);
|
|
expect(
|
|
router.takeIfReady(connected: true, channelTableBusy: true),
|
|
isA<ContactShareLink>(),
|
|
);
|
|
});
|
|
|
|
test('only the latest link is held, and it is announced afresh', () {
|
|
final router = LinkRouter()..handle(_channelLink);
|
|
router.markPendingAnnounced();
|
|
expect(router.pendingAnnounced, isTrue);
|
|
router.handle(_contactLink);
|
|
expect(router.pending, isA<ContactShareLink>());
|
|
expect(router.pendingAnnounced, isFalse);
|
|
});
|
|
|
|
test('a malformed link does not replace a held one', () {
|
|
final router = LinkRouter()..handle(_channelLink);
|
|
router.handle('meshcore://nope');
|
|
expect(router.pending, isA<ChannelShareLink>());
|
|
});
|
|
});
|
|
}
|