Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vpn connection detector tests #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# This is a generated file; do not edit or check into version control.
connectivity_plus=/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/
connectivity_plus=C:\\Users\\Ahmod\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\connectivity_plus-4.0.2\\
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus","path":"/Users/bazie/.pub-cache/hosted/pub.dev/connectivity_plus-4.0.2/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":[]}],"date_created":"2023-08-28 01:18:46.159993","version":"3.13.0"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus","path":"C:\\\\Users\\\\Ahmod\\\\AppData\\\\Local\\\\Pub\\\\Cache\\\\hosted\\\\pub.dev\\\\connectivity_plus-4.0.2\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":[]}],"date_created":"2023-09-15 13:00:08.378147","version":"3.13.4"}
8 changes: 6 additions & 2 deletions lib/vpn_connection_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/cupertino.dart';

///[VpnConnectionState] has two values [connected],[disconnected]
///return [VpnConnectionState.connected] if vpn connection is active
Expand Down Expand Up @@ -29,7 +30,7 @@ class VpnConnectionDetector {
_connectivitySubscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) async {
await _checkVpnStatus();
await checkVpnStatus();
});
}
final StreamController<VpnConnectionState> _controller =
Expand Down Expand Up @@ -58,7 +59,7 @@ class VpnConnectionDetector {
Stream<VpnConnectionState> get vpnConnectionStream =>
_controller.stream.asBroadcastStream();

Future<void> _checkVpnStatus() async {
Future<void> checkVpnStatus() async {
final currentVpnStatus = await isVpnActive();
if (currentVpnStatus) {
_controller.add(VpnConnectionState.connected);
Expand All @@ -74,6 +75,9 @@ class VpnConnectionDetector {
_connectivitySubscription?.cancel();
}

static List<String> get commonVpnInterfaceNamePatterns =>
_commonVpnInterfaceNamePatterns;

static final List<String> _commonVpnInterfaceNamePatterns = [
'tun', // Linux/Unix TUN interface
'tap', // Linux/Unix TAP interface
Expand Down
31 changes: 31 additions & 0 deletions test/vpn_connection_detector_test.dart
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:vpn_connection_detector/vpn_connection_detector.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group(' Tests', () {
late VpnConnectionDetector vpnConnectionDetector;

setUp(() {
vpnConnectionDetector = VpnConnectionDetector();
});

tearDown(() {
vpnConnectionDetector.dispose();
});

test('Initial connection status is disconnected', () {
/** vpnConnectionStream is not returning any value, to get a value, there is a need to call
checkVpnStatus by making it public **/
vpnConnectionDetector.checkVpnStatus();

expect(vpnConnectionDetector.vpnConnectionStream,
emits(VpnConnectionState.disconnected));
});

test('Check VPN status when not connected', () async {
final isConnected = await VpnConnectionDetector.isVpnActive();
expect(isConnected, false);
});
});
}