Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NetworkInfoPlusLinuxPlugin extends NetworkInfoPlatform {

String? _getSubnetMask(List<Map<String, dynamic>>? data) {
final prefix = data?.firstOrNull?['prefix'] as int;
final mask = 0xffffffff >> (32 - prefix);
final mask = (0xffffffff << (32 - prefix)) & 0xffffffff;
return mask.toIpString();
}

Expand All @@ -113,17 +113,26 @@ class NetworkInfoPlusLinuxPlugin extends NetworkInfoPlatform {
}

extension _IpInt on int {
int byteAt(int i) => (this >> (i * 8)) & 0xff;
String toIpString() => '${byteAt(0)}.${byteAt(1)}.${byteAt(2)}.${byteAt(3)}';
/// Returns the i-th octet in network (big-endian) order.
/// i must be 0..3.
int octet(int i) {
assert(i >= 0 && i <= 3);
return (this >> (24 - i * 8)) & 0xff;
}

String toIpString() {
final v = this & 0xFFFFFFFF;
return '${v.octet(0)}.${v.octet(1)}.${v.octet(2)}.${v.octet(3)}';
}
}

extension _IpString on String {
int toIpInt() {
final parts = split('.');
return parts.intAtOrZero(3) << 24 |
parts.intAtOrZero(2) << 16 |
parts.intAtOrZero(1) << 8 |
parts.intAtOrZero(0);
return parts.intAtOrZero(0) << 24 |
parts.intAtOrZero(1) << 16 |
parts.intAtOrZero(2) << 8 |
parts.intAtOrZero(3);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,79 @@
@TestOn('linux')
library;

import 'package:flutter_test/flutter_test.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:network_info_plus_platform_interface/network_info_plus_platform_interface.dart';
import 'package:nm/nm.dart';

void main() {
test('registered instance', () {
NetworkInfoPlusLinuxPlugin.registerWith();
expect(NetworkInfoPlatform.instance, isA<NetworkInfoPlusLinuxPlugin>());
});

group('IPv4 subnet mask and broadcast', () {
// address, prefix, expected mask, expected broadcast
const cases = [
('192.168.1.10', 24, '255.255.255.0', '192.168.1.255'),
('192.168.1.10', 22, '255.255.252.0', '192.168.3.255'),
('192.168.1.10', 25, '255.255.255.128', '192.168.1.127'),
('10.1.2.3', 8, '255.0.0.0', '10.255.255.255'),
('172.16.5.4', 12, '255.240.0.0', '172.31.255.255'),
('200.100.50.25', 20, '255.255.240.0', '200.100.63.255'),
('10.0.0.1', 30, '255.255.255.252', '10.0.0.3'),
('192.168.1.10', 32, '255.255.255.255', '192.168.1.10'),
('192.168.1.10', 0, '0.0.0.0', '255.255.255.255'),
];

for (final (address, prefix, mask, broadcast) in cases) {
test('$address/$prefix', () async {
final plugin = NetworkInfoPlusLinuxPlugin()
..createClient = () => _FakeClient(address: address, prefix: prefix);

expect(await plugin.getWifiSubmask(), mask);
expect(await plugin.getWifiBroadcast(), broadcast);
});
}
});

test('wifi IP and gateway are passed through', () async {
final plugin = NetworkInfoPlusLinuxPlugin()
..createClient = () => _FakeClient(address: '192.168.1.10', prefix: 22);

expect(await plugin.getWifiIP(), '192.168.1.10');
expect(await plugin.getWifiGatewayIP(), '192.168.0.1');
});
}

class _FakeClient extends Fake implements NetworkManagerClient {
_FakeClient({required String address, required int prefix})
: primaryConnection = _FakeConnection(
_FakeIp4Config([
{'address': address, 'prefix': prefix},
]),
);

@override
final NetworkManagerActiveConnection? primaryConnection;

@override
Future<void> connect() async {}

@override
Future<void> close() async {}
}

class _FakeConnection extends Fake implements NetworkManagerActiveConnection {
_FakeConnection(this.ip4Config);

@override
final NetworkManagerIP4Config? ip4Config;
}

class _FakeIp4Config extends Fake implements NetworkManagerIP4Config {
_FakeIp4Config(this.addressData);

@override
final List<Map<String, dynamic>> addressData;

@override
String get gateway => '192.168.0.1';
}
Loading