Import netquality 1.1.1
Initial import of the 1.1.1 source tree. Corresponds to Origin commit f9d97e368818acfe7dd44fa9fe5088b9629d0dff. Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
1 parent
948594e7c0
commit
f6ffbde518
93 files changed
+10570
-1
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:netquality/probe/endpoint_picker.dart';
|
||||
|
||||
void main() {
|
||||
test('a miss before any answer tries the next address', () {
|
||||
final v6 = InternetAddress('2001:db8::1');
|
||||
final v4 = InternetAddress('203.0.113.5');
|
||||
final picker = EndpointPicker([v6, v4]);
|
||||
expect(picker.current, v6);
|
||||
expect(picker.record(false), isTrue);
|
||||
expect(picker.current, v4);
|
||||
expect(picker.record(true), isFalse);
|
||||
expect(picker.current, v4);
|
||||
expect(picker.hasAnswered, isTrue);
|
||||
});
|
||||
|
||||
test('a working address survives two misses and leaves on the third', () {
|
||||
final v4 = InternetAddress('203.0.113.5');
|
||||
final v6 = InternetAddress('2001:db8::1');
|
||||
final picker = EndpointPicker([v4, v6]);
|
||||
expect(picker.record(true), isFalse);
|
||||
expect(picker.record(false), isFalse);
|
||||
expect(picker.record(false), isFalse);
|
||||
expect(picker.current, v4);
|
||||
expect(picker.record(false), isTrue);
|
||||
expect(picker.current, v6);
|
||||
expect(picker.hasAnswered, isFalse);
|
||||
expect(picker.record(false), isTrue);
|
||||
expect(picker.current, v4);
|
||||
});
|
||||
|
||||
test('one address is never abandoned', () {
|
||||
final only = InternetAddress('203.0.113.9');
|
||||
final picker = EndpointPicker([only]);
|
||||
expect(picker.record(false), isFalse);
|
||||
expect(picker.record(false), isFalse);
|
||||
expect(picker.current, only);
|
||||
});
|
||||
|
||||
test('198.18.0.0/15 is treated as a VPN fake address', () {
|
||||
expect(isClashFakeIp(InternetAddress('198.18.0.1')), isTrue);
|
||||
expect(isClashFakeIp(InternetAddress('198.19.255.254')), isTrue);
|
||||
expect(isClashFakeIp(InternetAddress('198.17.255.255')), isFalse);
|
||||
expect(isClashFakeIp(InternetAddress('198.20.0.1')), isFalse);
|
||||
expect(isClashFakeIp(InternetAddress('180.172.93.226')), isFalse);
|
||||
expect(isClashFakeIp(InternetAddress('2001:db8::1')), isFalse);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,498 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:netquality/metrics/sample_book.dart';
|
||||
import 'package:netquality/probe/game_codec.dart';
|
||||
import 'package:netquality/probe/probe_loop.dart';
|
||||
import 'package:netquality/probe/remote_client.dart';
|
||||
|
||||
late String binary;
|
||||
|
||||
void main() {
|
||||
setUpAll(() async {
|
||||
final dir = await Directory.systemTemp.createTemp('nq-node');
|
||||
binary = '${dir.path}/netquality-reflector';
|
||||
final serverDir = Directory('server').existsSync() ? 'server' : '../server';
|
||||
final build = await Process.run('go', [
|
||||
'build',
|
||||
'-o',
|
||||
binary,
|
||||
'.',
|
||||
], workingDirectory: serverDir);
|
||||
expect(build.exitCode, 0, reason: '${build.stdout}\n${build.stderr}');
|
||||
});
|
||||
|
||||
test('local nq tcp and udp get real rtt and do not call connect failure loss', () async {
|
||||
final reflector = await _reflector();
|
||||
addTearDown(() => reflector.process.kill());
|
||||
final tcp = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: reflector.tcp,
|
||||
profileId: 'nq-tcp',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 800),
|
||||
packetSize: 80,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 2,
|
||||
);
|
||||
expect(tcp.last.status, Outcome.ok);
|
||||
expect(tcp.last.rttMs, greaterThan(0));
|
||||
expect(tcp.last.summary.probeLossPct, 0);
|
||||
|
||||
final udp = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: reflector.udp,
|
||||
profileId: 'nq-udp',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 800),
|
||||
packetSize: 80,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 1,
|
||||
);
|
||||
expect(udp.single.status, Outcome.ok);
|
||||
expect(udp.single.summary.probeLossPct, 0);
|
||||
|
||||
final refused = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: 1,
|
||||
profileId: 'tcp-connect',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 400),
|
||||
packetSize: 64,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 1,
|
||||
);
|
||||
expect(refused.single.status, Outcome.refused);
|
||||
expect(refused.single.summary.failurePct, 100);
|
||||
expect(refused.single.summary.probeLossPct, isNull);
|
||||
});
|
||||
|
||||
test('silence is unknown, games answer, and a remote node measures rtt', () async {
|
||||
final hole = await RawDatagramSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
addTearDown(hole.close);
|
||||
hole.listen((event) {
|
||||
if (event == RawSocketEvent.read) {
|
||||
hole.receive();
|
||||
}
|
||||
});
|
||||
final unknown = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: hole.port,
|
||||
profileId: 'udp-generic',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 200),
|
||||
packetSize: 16,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 1,
|
||||
);
|
||||
expect(unknown.single.status, Outcome.unknown);
|
||||
expect(unknown.single.summary.probeLossPct, isNull);
|
||||
expect(unknown.single.summary.unknownPct, 100);
|
||||
expect(unknown.single.detail['address'], '127.0.0.1');
|
||||
|
||||
final java = await _javaServer();
|
||||
addTearDown(java.close);
|
||||
final javaSample = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: java.port,
|
||||
profileId: 'minecraft-java',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 800),
|
||||
packetSize: 64,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 1,
|
||||
);
|
||||
expect(javaSample.single.status, Outcome.ok);
|
||||
expect(javaSample.single.detail['motd'], 'Hello');
|
||||
expect(javaSample.single.rttMs, greaterThan(0));
|
||||
expect(javaSample.single.summary.failurePct, 0);
|
||||
|
||||
final bedrock = await _bedrockServer();
|
||||
addTearDown(bedrock.close);
|
||||
final bedrockSample = await _collect(
|
||||
ProbeRequest(
|
||||
host: '127.0.0.1',
|
||||
port: bedrock.port,
|
||||
profileId: 'minecraft-bedrock',
|
||||
interval: const Duration(milliseconds: 200),
|
||||
timeout: const Duration(milliseconds: 800),
|
||||
packetSize: 64,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 1,
|
||||
);
|
||||
expect(bedrockSample.single.status, Outcome.ok);
|
||||
expect(bedrockSample.single.detail['motd'], 'Bed');
|
||||
|
||||
final node = await _node();
|
||||
addTearDown(node.kill);
|
||||
final samples = <ProbeSample>[];
|
||||
final remote = RemoteProbe();
|
||||
final done = Completer<void>();
|
||||
unawaited(() async {
|
||||
try {
|
||||
await remote.run(
|
||||
baseUrl: 'http://127.0.0.1:${node.control}',
|
||||
nodeToken: '',
|
||||
body: ProbeBody(
|
||||
target: '127.0.0.1',
|
||||
port: node.udp,
|
||||
profile: 'nq-udp',
|
||||
intervalMs: 200,
|
||||
timeoutMs: 800,
|
||||
durationSec: 3,
|
||||
packetSize: 80,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
onSample: (sample) {
|
||||
samples.add(sample);
|
||||
if (samples.length == 1 && !done.isCompleted) {
|
||||
done.complete();
|
||||
}
|
||||
},
|
||||
cancelled: () => done.isCompleted,
|
||||
);
|
||||
} finally {
|
||||
if (!done.isCompleted) {
|
||||
done.complete();
|
||||
}
|
||||
}
|
||||
}());
|
||||
await done.future.timeout(const Duration(seconds: 5));
|
||||
await remote.stop('');
|
||||
expect(samples, isNotEmpty);
|
||||
expect(samples.first.status, Outcome.ok);
|
||||
expect(samples.first.rttMs, greaterThan(0));
|
||||
expect(samples.first.summary.probeLossPct, 0);
|
||||
});
|
||||
|
||||
test('plain udp leaves a silent address and keeps one that answers', () async {
|
||||
final drop = await RawDatagramSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
addTearDown(drop.close);
|
||||
final port = drop.port;
|
||||
drop.listen((event) {
|
||||
if (event == RawSocketEvent.read) {
|
||||
drop.receive();
|
||||
}
|
||||
});
|
||||
final RawDatagramSocket echo;
|
||||
try {
|
||||
echo = await RawDatagramSocket.bind(InternetAddress.loopbackIPv6, port);
|
||||
} on SocketException {
|
||||
markTestSkipped('IPv6 loopback is not available');
|
||||
return;
|
||||
}
|
||||
addTearDown(echo.close);
|
||||
echo.listen((event) {
|
||||
if (event != RawSocketEvent.read) {
|
||||
return;
|
||||
}
|
||||
final datagram = echo.receive();
|
||||
if (datagram == null) {
|
||||
return;
|
||||
}
|
||||
echo.send(datagram.data, datagram.address, datagram.port);
|
||||
});
|
||||
|
||||
final seen = <String>[];
|
||||
final samples = await _collect(
|
||||
ProbeRequest(
|
||||
host: 'example.test',
|
||||
port: port,
|
||||
profileId: 'udp-generic',
|
||||
interval: const Duration(milliseconds: 40),
|
||||
timeout: const Duration(milliseconds: 250),
|
||||
packetSize: 16,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 2,
|
||||
loop: ProbeLoop(
|
||||
lookup: (host) async {
|
||||
expect(host, 'example.test');
|
||||
return [InternetAddress('127.0.0.1'), InternetAddress('::1')];
|
||||
},
|
||||
onEndpoint: (address) => seen.add(address.address),
|
||||
),
|
||||
);
|
||||
expect(samples.first.status, Outcome.unknown);
|
||||
expect(samples.first.detail['address'], '127.0.0.1');
|
||||
expect(samples.last.status, Outcome.ok);
|
||||
expect(samples.last.detail['address'], '::1');
|
||||
expect(seen, ['127.0.0.1', '::1']);
|
||||
});
|
||||
|
||||
test('a responding address is not replaced while it still answers', () async {
|
||||
final echo = await RawDatagramSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
addTearDown(echo.close);
|
||||
echo.listen((event) {
|
||||
if (event != RawSocketEvent.read) {
|
||||
return;
|
||||
}
|
||||
final datagram = echo.receive();
|
||||
if (datagram == null) {
|
||||
return;
|
||||
}
|
||||
echo.send(datagram.data, datagram.address, datagram.port);
|
||||
});
|
||||
final samples = await _collect(
|
||||
ProbeRequest(
|
||||
host: 'example.test',
|
||||
port: echo.port,
|
||||
profileId: 'udp-generic',
|
||||
interval: const Duration(milliseconds: 40),
|
||||
timeout: const Duration(milliseconds: 300),
|
||||
packetSize: 16,
|
||||
echoToken: '',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
),
|
||||
count: 3,
|
||||
loop: ProbeLoop(
|
||||
lookup: (_) async => [
|
||||
InternetAddress('127.0.0.1'),
|
||||
InternetAddress('2001:db8::1'),
|
||||
],
|
||||
),
|
||||
);
|
||||
expect(samples.map((sample) => sample.status), everyElement(Outcome.ok));
|
||||
expect(samples.map((sample) => sample.detail['address']), everyElement('127.0.0.1'));
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<ProbeSample>> _collect(
|
||||
ProbeRequest request, {
|
||||
required int count,
|
||||
ProbeLoop? loop,
|
||||
}) async {
|
||||
final probe = loop ?? ProbeLoop();
|
||||
final samples = <ProbeSample>[];
|
||||
var stop = false;
|
||||
await probe
|
||||
.run(
|
||||
request: request,
|
||||
onSample: (sample) {
|
||||
samples.add(sample);
|
||||
if (samples.length >= count) {
|
||||
stop = true;
|
||||
}
|
||||
},
|
||||
cancelled: () => stop,
|
||||
)
|
||||
.timeout(const Duration(seconds: 6));
|
||||
return samples;
|
||||
}
|
||||
|
||||
Future<({Process process, int tcp, int udp})> _reflector() async {
|
||||
final tcp = await _freeTcp();
|
||||
final udp = await _freeUdp();
|
||||
final process = await Process.start(binary, [
|
||||
'-bind',
|
||||
'127.0.0.1',
|
||||
'-tcp-port',
|
||||
'$tcp',
|
||||
'-udp-port',
|
||||
'$udp',
|
||||
'-mode',
|
||||
'reflector',
|
||||
]);
|
||||
await _waitListen(process);
|
||||
return (process: process, tcp: tcp, udp: udp);
|
||||
}
|
||||
|
||||
Future<({Process process, int control, int udp, void Function() kill})> _node() async {
|
||||
final udp = await _freeUdp();
|
||||
final control = await _freeTcp();
|
||||
final process = await Process.start(binary, [
|
||||
'-bind',
|
||||
'127.0.0.1',
|
||||
'-tcp-port',
|
||||
'0',
|
||||
'-udp-port',
|
||||
'$udp',
|
||||
'-mode',
|
||||
'both',
|
||||
'-control-addr',
|
||||
'127.0.0.1:$control',
|
||||
'-allow-private',
|
||||
]);
|
||||
final ready = Completer<void>();
|
||||
process.stderr.transform(utf8.decoder).listen((text) {
|
||||
if (text.contains('node control listening') && !ready.isCompleted) {
|
||||
ready.complete();
|
||||
}
|
||||
});
|
||||
process.stdout.transform(utf8.decoder).listen((_) {});
|
||||
await ready.future.timeout(const Duration(seconds: 3));
|
||||
return (
|
||||
process: process,
|
||||
control: control,
|
||||
udp: udp,
|
||||
kill: () {
|
||||
process.kill();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _waitListen(Process process) async {
|
||||
final ready = Completer<void>();
|
||||
process.stderr.transform(utf8.decoder).listen((text) {
|
||||
if (text.contains('listening') && !ready.isCompleted) {
|
||||
ready.complete();
|
||||
}
|
||||
});
|
||||
process.stdout.transform(utf8.decoder).listen((_) {});
|
||||
await ready.future.timeout(const Duration(seconds: 3));
|
||||
}
|
||||
|
||||
Future<int> _freeTcp() async {
|
||||
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
final port = server.port;
|
||||
await server.close();
|
||||
return port;
|
||||
}
|
||||
|
||||
Future<int> _freeUdp() async {
|
||||
final socket = await RawDatagramSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
final port = socket.port;
|
||||
socket.close();
|
||||
return port;
|
||||
}
|
||||
|
||||
Future<({int port, Future<void> Function() close})> _javaServer() async {
|
||||
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
final sub = server.listen((socket) async {
|
||||
final reader = _Reader(socket);
|
||||
await reader.next();
|
||||
await reader.next();
|
||||
const status =
|
||||
'{"version":{"name":"1.21.1","protocol":767},"players":{"online":3,"max":20},"description":{"text":"Hello"}}';
|
||||
final payload = BytesBuilder(copy: false)
|
||||
..addByte(0x00)
|
||||
..add(mcString(status));
|
||||
socket.add(mcPacket(payload.toBytes()));
|
||||
final ping = await reader.next();
|
||||
if (ping != null) {
|
||||
socket.add(mcPacket(ping));
|
||||
}
|
||||
await socket.close();
|
||||
});
|
||||
return (
|
||||
port: server.port,
|
||||
close: () async {
|
||||
await sub.cancel();
|
||||
await server.close();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<({int port, void Function() close})> _bedrockServer() async {
|
||||
final socket = await RawDatagramSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
final sub = socket.listen((event) {
|
||||
if (event != RawSocketEvent.read) {
|
||||
return;
|
||||
}
|
||||
final datagram = socket.receive();
|
||||
if (datagram == null || datagram.data.length < 9) {
|
||||
return;
|
||||
}
|
||||
final ts = datagram.data.sublist(1, 9);
|
||||
const motd = 'MCPE;Bed;844;1.21.50;1;10;0;world;Survival;1;19132;0;';
|
||||
final body = BytesBuilder(copy: false)
|
||||
..addByte(0x1c)
|
||||
..add(ts)
|
||||
..add(const [0, 0, 0, 0, 0, 0, 0, 99])
|
||||
..add(rakMagic)
|
||||
..addByte(0)
|
||||
..addByte(motd.length)
|
||||
..add(motd.codeUnits);
|
||||
socket.send(body.toBytes(), datagram.address, datagram.port);
|
||||
});
|
||||
return (
|
||||
port: socket.port,
|
||||
close: () {
|
||||
sub.cancel();
|
||||
socket.close();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
class _Reader {
|
||||
_Reader(this.socket) {
|
||||
socket.listen((data) {
|
||||
_buf.add(data);
|
||||
_pending?.complete();
|
||||
_pending = null;
|
||||
});
|
||||
}
|
||||
|
||||
final Socket socket;
|
||||
final BytesBuilder _buf = BytesBuilder(copy: false);
|
||||
Completer<void>? _pending;
|
||||
|
||||
Future<Uint8List?> next() async {
|
||||
final deadline = DateTime.now().add(const Duration(seconds: 2));
|
||||
while (true) {
|
||||
final bytes = _buf.toBytes();
|
||||
final packet = takeMcPacket(bytes);
|
||||
if (packet != null) {
|
||||
_buf.clear();
|
||||
if (packet.used < bytes.length) {
|
||||
_buf.add(Uint8List.sublistView(bytes, packet.used));
|
||||
}
|
||||
return packet.payload;
|
||||
}
|
||||
final left = deadline.difference(DateTime.now());
|
||||
if (left <= Duration.zero) {
|
||||
return null;
|
||||
}
|
||||
final waiter = Completer<void>();
|
||||
_pending = waiter;
|
||||
if (takeMcPacket(_buf.toBytes()) != null) {
|
||||
waiter.complete();
|
||||
}
|
||||
try {
|
||||
await waiter.future.timeout(left);
|
||||
} on TimeoutException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:netquality/protocol/probe_packet.dart';
|
||||
|
||||
void main() {
|
||||
test('golden request matches the server fixture', () {
|
||||
final packet = ProbePacket(
|
||||
type: ProbePacket.typeRequest,
|
||||
sequence: 1,
|
||||
clientSendUnixNano: 1000,
|
||||
payload: Uint8List.fromList([1, 2, 3, 4]),
|
||||
);
|
||||
final hex = packet
|
||||
.encode()
|
||||
.map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
||||
.join();
|
||||
expect(
|
||||
hex,
|
||||
'4e5130310101000001000000e803000000000000'
|
||||
'0000000000000000000000000000000000000000'
|
||||
'0000000000000000000000000000000000000000'
|
||||
'040001020304',
|
||||
);
|
||||
final back = ProbePacket.decode(packet.encode());
|
||||
expect(back.sequence, 1);
|
||||
expect(back.clientSendUnixNano, 1000);
|
||||
expect(back.type, ProbePacket.typeRequest);
|
||||
expect(back.payload, [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
test('token is zero padded in the header', () {
|
||||
final token = ProbePacket.encodeToken('demo');
|
||||
expect(token, hasLength(32));
|
||||
expect(token.sublist(0, 4), [0x64, 0x65, 0x6d, 0x6f]);
|
||||
expect(token.sublist(4), everyElement(0));
|
||||
expect(() => ProbePacket.encodeToken('x' * 33), throwsA(isA<ProbePacketException>()));
|
||||
});
|
||||
|
||||
test('frame buffer splits partial and concatenated TCP frames', () {
|
||||
final packet = ProbePacket(
|
||||
type: ProbePacket.typeReply,
|
||||
sequence: 9,
|
||||
clientSendUnixNano: 50,
|
||||
serverUnixNano: 80,
|
||||
payload: Uint8List.fromList([9, 8, 7]),
|
||||
).encode();
|
||||
final buffer = FrameBuffer();
|
||||
expect(buffer.add(packet.sublist(0, 20)), isEmpty);
|
||||
expect(buffer.add(packet.sublist(20, 40)), isEmpty);
|
||||
final one = buffer.add(packet.sublist(40));
|
||||
expect(one, hasLength(1));
|
||||
expect(ProbePacket.decode(one.single).sequence, 9);
|
||||
expect(ProbePacket.decode(one.single).serverUnixNano, 80);
|
||||
|
||||
final both = Uint8List.fromList([...packet, ...packet]);
|
||||
final two = FrameBuffer().add(both);
|
||||
expect(two, hasLength(2));
|
||||
expect(ProbePacket.decode(two[1]).payload, [9, 8, 7]);
|
||||
});
|
||||
|
||||
test('rejects a short or trailing datagram', () {
|
||||
final packet = ProbePacket(
|
||||
type: ProbePacket.typeRequest,
|
||||
sequence: 1,
|
||||
clientSendUnixNano: 1,
|
||||
).encode();
|
||||
expect(
|
||||
() => ProbePacket.decode(Uint8List.sublistView(packet, 0, 10)),
|
||||
throwsA(isA<ProbePacketException>()),
|
||||
);
|
||||
final extra = Uint8List(packet.length + 1)..setRange(0, packet.length, packet);
|
||||
expect(() => ProbePacket.decode(extra), throwsA(isA<ProbePacketException>()));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:netquality/metrics/sample_book.dart';
|
||||
import 'package:netquality/models/session_config.dart';
|
||||
import 'package:netquality/probe/game_codec.dart';
|
||||
import 'package:netquality/probe/remote_client.dart';
|
||||
|
||||
void main() {
|
||||
test('pending work is not a rate, and connect failure is not probe loss', () {
|
||||
final book = SampleBook('nq-tcp');
|
||||
expect(book.summary().probeLossPct, isNull);
|
||||
expect(book.summary().completed, 0);
|
||||
book.add(
|
||||
const Outcome(status: Outcome.refused, kind: Outcome.connect, reconnect: true),
|
||||
);
|
||||
expect(book.summary().probeLossPct, isNull);
|
||||
expect(book.summary().reconnects, 1);
|
||||
book.add(const Outcome(status: Outcome.timeout, kind: Outcome.probe));
|
||||
book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 1));
|
||||
book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 2));
|
||||
book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 1.5));
|
||||
expect(book.summary().probeLossPct, 25);
|
||||
expect(book.summary().jitterMs, closeTo(0.08984375, 0.000001));
|
||||
});
|
||||
|
||||
test('generic udp silence is unknown, not loss', () {
|
||||
final book = SampleBook('udp-generic');
|
||||
book.add(const Outcome(status: Outcome.unknown, kind: Outcome.query));
|
||||
final summary = book.summary();
|
||||
expect(summary.probeLossPct, isNull);
|
||||
expect(summary.unknownPct, 100);
|
||||
});
|
||||
|
||||
test('node url rejects embedded secrets and job body keeps tokens apart', () {
|
||||
expect(normalizeNodeUrl('https://user:secret@probe.example/node'), isNull);
|
||||
expect(
|
||||
normalizeNodeUrl('https://probe.example:8443/nq?token=secret'),
|
||||
'https://probe.example:8443/nq',
|
||||
);
|
||||
expect(normalizeNodeUrl('http://127.0.0.1:8443'), 'http://127.0.0.1:8443');
|
||||
const body = ProbeBody(
|
||||
target: '203.0.113.10',
|
||||
port: 19090,
|
||||
profile: 'nq-tcp',
|
||||
intervalMs: 300,
|
||||
timeoutMs: 1000,
|
||||
durationSec: 30,
|
||||
packetSize: 128,
|
||||
echoToken: 'echo-secret',
|
||||
dnsName: '',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
);
|
||||
final json = body.toJson();
|
||||
expect(json.containsKey('nodeToken'), isFalse);
|
||||
expect(json['echoToken'], 'echo-secret');
|
||||
expect(jsonEncodeContainsToken(json), isFalse);
|
||||
});
|
||||
|
||||
test('minecraft and raknet builders match the protocol', () {
|
||||
final handshake = javaHandshake('127.0.0.1', 25565);
|
||||
expect(handshake.sublist(2, 4), [0xff, 0x05]);
|
||||
final status = minecraftStatusDetail(
|
||||
'{"version":{"name":"1.21.1"},"players":{"online":3,"max":20},"description":{"text":"Hello","extra":[{"text":"!"}]}}',
|
||||
);
|
||||
expect(status['motd'], 'Hello!');
|
||||
expect(status['version'], '1.21.1');
|
||||
expect(status['players'], '3/20');
|
||||
|
||||
const motd = 'MCPE;Bed;844;1.21.50;1;10;0;';
|
||||
final pong = Uint8List.fromList([
|
||||
0x1c,
|
||||
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
||||
0, 0, 0, 0, 0, 0, 0, 99,
|
||||
...rakMagic,
|
||||
0, motd.length,
|
||||
...motd.codeUnits,
|
||||
]);
|
||||
final detail = parseRakPong(pong, 0x1122334455667788);
|
||||
expect(detail?['motd'], 'Bed');
|
||||
expect(detail?['version'], '1.21.50');
|
||||
expect(detail?['players'], '1/10');
|
||||
final bad = Uint8List.fromList(pong);
|
||||
bad[17] = 0x11;
|
||||
expect(parseRakPong(bad, 0x1122334455667788), isNull);
|
||||
final query = dnsQuery(7, 'example.com');
|
||||
expect(query[0], 0);
|
||||
expect(query[1], 7);
|
||||
expect((query[2] & 0x80), 0);
|
||||
});
|
||||
}
|
||||
|
||||
bool jsonEncodeContainsToken(Map<String, Object?> json) {
|
||||
return json.keys.contains('nodeToken') || json.values.contains('node-control-secret');
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:netquality/main.dart';
|
||||
import 'package:netquality/models/session_config.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('opens on local source with an empty chart', (tester) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
tester.view.physicalSize = const Size(1280, 1400);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
await tester.pumpWidget(const NetQualityApp());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('网络质量探测'), findsOneWidget);
|
||||
expect(find.text('探测来源'), findsOneWidget);
|
||||
expect(find.text('本机'), findsWidgets);
|
||||
expect(find.text('远程节点'), findsWidgets);
|
||||
expect(find.text('开始探测'), findsOneWidget);
|
||||
expect(find.text('暂无已完成的探测'), findsNWidgets(2));
|
||||
expect(find.textContaining('127.0.0.1'), findsWidgets);
|
||||
});
|
||||
|
||||
test('settings round trip keeps the node and drops nothing secret into json keys', () {
|
||||
const config = SessionConfig(
|
||||
remote: true,
|
||||
nodeId: 'n1',
|
||||
nodes: [RemoteNode(id: 'n1', name: '机房', baseUrl: 'https://probe.example:8443')],
|
||||
host: '203.0.113.8',
|
||||
port: 25565,
|
||||
profileId: 'minecraft-java',
|
||||
intervalMs: 300,
|
||||
timeoutMs: 1000,
|
||||
durationSec: 60,
|
||||
packetSize: 128,
|
||||
dnsName: 'example.com',
|
||||
payloadHex: '',
|
||||
expectHex: '',
|
||||
);
|
||||
final again = SessionConfig.fromJson(config.toJson());
|
||||
expect(again.host, '203.0.113.8');
|
||||
expect(again.profileId, 'minecraft-java');
|
||||
expect(again.node?.name, '机房');
|
||||
expect(again.toJson().containsKey('token'), isFalse);
|
||||
expect(again.toJson().containsKey('nodeToken'), isFalse);
|
||||
expect(SessionConfig.defaults.validate(), isNull);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user