|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:network_tools/network_tools.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:network_tools_flutter/network_tools_flutter.dart'; |
| 6 | +import 'package:universal_io/io.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + // Logger.root.level = Level.FINE; |
| 10 | + // Logger.root.onRecord.listen((record) { |
| 11 | + // print( |
| 12 | + // '${DateFormat.Hms().format(record.time)}: ${record.level.name}: ${record.loggerName}: ${record.message}', |
| 13 | + // ); |
| 14 | + // }); |
| 15 | + int port = 0; // keep this value between 1-2034 |
| 16 | + final List<ActiveHost> hostsWithOpenPort = []; |
| 17 | + late ServerSocket server; |
| 18 | + // Fetching interfaceIp and hostIp |
| 19 | + setUpAll(() async { |
| 20 | + //open a port in shared way because of hostscanner using same, |
| 21 | + //if passed false then two hosts come up in search and breaks test. |
| 22 | + server = |
| 23 | + await ServerSocket.bind(InternetAddress.anyIPv4, port, shared: true); |
| 24 | + port = server.port; |
| 25 | + final interfaceList = |
| 26 | + await NetworkInterface.list(); //will give interface list |
| 27 | + if (interfaceList.isNotEmpty) { |
| 28 | + final localInterface = |
| 29 | + interfaceList.elementAt(0); //fetching first interface like en0/eth0 |
| 30 | + if (localInterface.addresses.isNotEmpty) { |
| 31 | + final address = localInterface.addresses |
| 32 | + .elementAt(0) |
| 33 | + .address; //gives IP address of GHA local machine. |
| 34 | + final interfaceIp = address.substring(0, address.lastIndexOf('.')); |
| 35 | + //ssh should be running at least in any host |
| 36 | + await for (final host |
| 37 | + in HostScanner.scanDevicesForSinglePort(interfaceIp, port)) { |
| 38 | + hostsWithOpenPort.add(host); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + group('Testing Port Scanner', () { |
| 45 | + test('Running scanPortsForSingleDevice tests', () { |
| 46 | + for (final activeHost in hostsWithOpenPort) { |
| 47 | + final port = activeHost.openPorts.elementAt(0).port; |
| 48 | + expectLater( |
| 49 | + PortScannerFlutter.scanPortsForSingleDevice( |
| 50 | + activeHost.address, |
| 51 | + startPort: port - 1, |
| 52 | + endPort: port + 1, |
| 53 | + ), |
| 54 | + emitsThrough( |
| 55 | + isA<ActiveHost>().having( |
| 56 | + (p0) => p0.openPorts.contains(OpenPort(port)), |
| 57 | + "Should match host having same open port", |
| 58 | + equals(true), |
| 59 | + ), |
| 60 | + ), |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + test('Running connectToPort tests', () { |
| 66 | + for (final activeHost in hostsWithOpenPort) { |
| 67 | + expectLater( |
| 68 | + PortScannerFlutter.connectToPort( |
| 69 | + address: activeHost.address, |
| 70 | + port: port, |
| 71 | + timeout: const Duration(seconds: 5), |
| 72 | + activeHostsController: StreamController<ActiveHost>(), |
| 73 | + ), |
| 74 | + completion( |
| 75 | + isA<ActiveHost>().having( |
| 76 | + (p0) => p0.openPorts.contains(OpenPort(port)), |
| 77 | + "Should match host having same open port", |
| 78 | + equals(true), |
| 79 | + ), |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | + }); |
| 84 | + test('Running customDiscover tests', () { |
| 85 | + for (final activeHost in hostsWithOpenPort) { |
| 86 | + expectLater( |
| 87 | + PortScannerFlutter.customDiscover(activeHost.address, |
| 88 | + portList: [port - 1, port, port + 1]), |
| 89 | + emits(isA<ActiveHost>()), |
| 90 | + ); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + test('Running isOpen tests', () { |
| 95 | + for (final activeHost in hostsWithOpenPort) { |
| 96 | + expectLater( |
| 97 | + PortScannerFlutter.isOpen(activeHost.address, port), |
| 98 | + completion( |
| 99 | + isA<ActiveHost>().having( |
| 100 | + (p0) => p0.openPorts.contains(OpenPort(port)), |
| 101 | + "Should match host having same open port", |
| 102 | + equals(true), |
| 103 | + ), |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + tearDownAll(() { |
| 111 | + server.close(); |
| 112 | + }); |
| 113 | +} |
0 commit comments