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.
247 lines
9.1 KiB
247 lines
9.1 KiB
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:meshcore_open/helpers/share_link_forms.dart';
|
|
import 'package:meshcore_open/models/channel.dart';
|
|
import 'package:meshcore_open/models/contact.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',
|
|
// Longer than any radio packet: not an advert.
|
|
'meshcore://${'ab' * 256}',
|
|
'meshcore://zz',
|
|
'',
|
|
]) {
|
|
expect(parseShareLink(bad), isNull, reason: bad);
|
|
}
|
|
});
|
|
});
|
|
|
|
group('offband.app links (#1309)', () {
|
|
String https(String host, String kind, String fields) =>
|
|
'https://$host/add/$kind#$fields';
|
|
|
|
test('read the same as their meshcore:// twins, on both hosts', () {
|
|
for (final host in shareLinkHosts) {
|
|
final channel =
|
|
parseShareLink(
|
|
https(
|
|
host,
|
|
'channel',
|
|
'name=%23echo&secret=5d25cf40b1f5b4a7eb0cf9703634a948',
|
|
),
|
|
)
|
|
as ChannelShareLink;
|
|
final twin = parseShareLink(_channelLink) as ChannelShareLink;
|
|
expect(channel.channel.name, twin.channel.name);
|
|
expect(channel.channel.pskHex, twin.channel.pskHex);
|
|
|
|
final hashtag =
|
|
parseShareLink(https(host, 'channel', 'name=%23example'))
|
|
as ChannelShareLink;
|
|
expect(hashtag.channel.name, '#example');
|
|
|
|
final link = https(host, 'contact', 'name=Bob&public_key=$_key&type=1');
|
|
final contact = parseShareLink(link) as ContactShareLink;
|
|
expect(contact.contact.name, 'Bob');
|
|
// Held as received; the add-contact dialog reads either form (#1311).
|
|
expect(contact.uri, link);
|
|
expect(Contact.fromShareUri(contact.uri)?.publicKeyHex, _key);
|
|
}
|
|
});
|
|
|
|
test('read a hand-typed path in any case', () {
|
|
expect(
|
|
parseShareLink('https://offband.app/Add/Channel#name=%23example'),
|
|
isA<ChannelShareLink>(),
|
|
);
|
|
});
|
|
|
|
test('keep a name with reserved characters intact', () {
|
|
const secret = '5d25cf40b1f5b4a7eb0cf9703634a948';
|
|
for (final name in ['a&b', 'x=y', '50% off', 'one+two', 'a b', '#c#d']) {
|
|
final link =
|
|
Channel(
|
|
index: 0,
|
|
name: name,
|
|
psk: Channel.parsePskHex(secret),
|
|
).toShareUri().replaceFirst(
|
|
'meshcore://channel/add?',
|
|
'https://offband.app/add/channel#',
|
|
);
|
|
final parsed = parseShareLink(link) as ChannelShareLink;
|
|
expect(parsed.channel.name, name, reason: link);
|
|
expect(parsed.channel.pskHex, secret, reason: link);
|
|
}
|
|
});
|
|
|
|
test('accept the host in any case and a trailing slash', () {
|
|
expect(
|
|
parseShareLink('https://Offband.App/add/channel/#name=%23example'),
|
|
isA<ChannelShareLink>(),
|
|
);
|
|
});
|
|
|
|
test('anything else is not one', () {
|
|
for (final bad in [
|
|
'http://offband.app/add/channel#name=%23example',
|
|
'https://example.com/add/channel#name=%23example',
|
|
'https://offband.app.example.com/add/channel#name=%23example',
|
|
'https://offband.app:8443/add/channel#name=%23example',
|
|
'https://offband.app/add/channel',
|
|
'https://offband.app/add/channel#',
|
|
// Fields in the query would reach the server's logs.
|
|
'https://offband.app/add/channel?name=%23example',
|
|
'https://offband.app/add/channel?x=1#name=%23example',
|
|
'https://offband.app/add/group#name=%23example',
|
|
'https://offband.app/add/channel/more#name=%23example',
|
|
'https://offband.app/channel#name=%23example',
|
|
]) {
|
|
expect(meshcoreFormOf(bad), isNull, reason: bad);
|
|
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('from the OS, only share links are acted on (#1291)', () {
|
|
final router = LinkRouter();
|
|
// An ordinary launch, and on web the page's own address.
|
|
router.handleFromOs(Uri.parse('https://offband.app/'));
|
|
router.handleFromOs(Uri.parse('offband://open'));
|
|
expect(router.pending, isNull);
|
|
expect(router.takeRejected(), isNull, reason: 'not reported either');
|
|
|
|
router.handleFromOs(Uri.parse(_hashtagLink));
|
|
expect(router.pending, isA<ChannelShareLink>());
|
|
|
|
router.handleFromOs(Uri.parse('MESHCORE://channel/add?name=nope'));
|
|
expect(router.takeRejected(), isNotNull, reason: 'a broken share link');
|
|
});
|
|
|
|
test('an offband.app link from the OS is taken and cleared (#1309)', () {
|
|
var cleared = 0;
|
|
final router = LinkRouter(clearAddressLink: () => cleared++);
|
|
router.handleFromOs(
|
|
Uri.parse('https://offband.app/add/channel#name=%23example'),
|
|
);
|
|
expect(router.pending, isA<ChannelShareLink>());
|
|
expect(cleared, 1);
|
|
|
|
// An /add/ address with broken fields is still a share link: it is
|
|
// reported, and still cleared so a reload does not report it again.
|
|
router.handleFromOs(Uri.parse('https://offband.app/add/channel'));
|
|
expect(router.takeRejected(), isNotNull);
|
|
expect(cleared, 2);
|
|
|
|
// The site's other pages are not links.
|
|
router.handleFromOs(Uri.parse('https://offband.app/'));
|
|
router.handleFromOs(Uri.parse('https://offband.app/privacy#top'));
|
|
router.handleFromOs(Uri.parse('https://example.com/add/channel#x=1'));
|
|
expect(router.takeRejected(), isNull);
|
|
expect(cleared, 2);
|
|
});
|
|
|
|
test('a meshcore:// link from the OS clears nothing', () {
|
|
var cleared = 0;
|
|
LinkRouter(
|
|
clearAddressLink: () => cleared++,
|
|
).handleFromOs(Uri.parse(_hashtagLink));
|
|
expect(cleared, 0);
|
|
});
|
|
|
|
test('a malformed link does not replace a held one', () {
|
|
final router = LinkRouter()..handle(_channelLink);
|
|
router.handle('meshcore://nope');
|
|
expect(router.pending, isA<ChannelShareLink>());
|
|
});
|
|
});
|
|
}
|