From f728bbb6eb24268d7223ac4347ad2cd5f004e85c Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 10 Feb 2018 23:19:00 +0100 Subject: #8837 new handling of connection state LEVEL_VPNPAUSED - don't throw an illegal state exception --- app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'app/src') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 0da74872..855bfc64 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -24,6 +24,7 @@ import java.util.Observable; import de.blinkt.openvpn.core.ConnectionStatus; import de.blinkt.openvpn.core.LogItem; +import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; /** @@ -92,7 +93,13 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { currentEipLevel = EipLevel.CONNECTED; break; case LEVEL_VPNPAUSED: - throw new IllegalStateException("Ics-Openvpn's VPNPAUSED state is not supported by Bitmask"); + if (ProfileManager.getLastConnectedVpn().mPersistTun) { + //if persistTun is enabled, treat EipLevel as connecting as it *shouldn't* allow passing traffic in the clear... + currentEipLevel = EipLevel.CONNECTING; + } else { + //... if persistTun is not enabled, background network traffic will pass in the clear + currentEipLevel = EipLevel.DISCONNECTED; + } case LEVEL_CONNECTING_SERVER_REPLIED: case LEVEL_CONNECTING_NO_SERVER_REPLY_YET: case LEVEL_WAITING_FOR_USER_INPUT: -- cgit v1.2.3 From 757293ca946f1b8c25d7bf13fc9f70bf70b4d8c5 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Mon, 12 Feb 2018 13:08:24 +0100 Subject: #8837 update tests and fix VPN_Paused implementation for EipStatus --- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 3 +- .../se/leap/bitmaskclient/eip/EipStatusTest.java | 35 ++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 855bfc64..df252500 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -93,13 +93,14 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { currentEipLevel = EipLevel.CONNECTED; break; case LEVEL_VPNPAUSED: - if (ProfileManager.getLastConnectedVpn().mPersistTun) { + if (ProfileManager.getLastConnectedVpn() != null && ProfileManager.getLastConnectedVpn().mPersistTun) { //if persistTun is enabled, treat EipLevel as connecting as it *shouldn't* allow passing traffic in the clear... currentEipLevel = EipLevel.CONNECTING; } else { //... if persistTun is not enabled, background network traffic will pass in the clear currentEipLevel = EipLevel.DISCONNECTED; } + break; case LEVEL_CONNECTING_SERVER_REPLIED: case LEVEL_CONNECTING_NO_SERVER_REPLY_YET: case LEVEL_WAITING_FOR_USER_INPUT: diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java index 15085b46..f332b094 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java @@ -3,9 +3,12 @@ package se.leap.bitmaskclient.eip; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConnectionStatus; +import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.R; @@ -18,6 +21,8 @@ import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_VPNPAUSED; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT; import static de.blinkt.openvpn.core.ConnectionStatus.UNKNOWN_LEVEL; import static junit.framework.Assert.assertTrue; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; import static se.leap.bitmaskclient.eip.EipStatus.EipLevel.CONNECTING; import static se.leap.bitmaskclient.eip.EipStatus.EipLevel.DISCONNECTED; import static se.leap.bitmaskclient.eip.EipStatus.EipLevel.UNKNOWN; @@ -26,7 +31,8 @@ import static se.leap.bitmaskclient.eip.EipStatus.EipLevel.UNKNOWN; * Created by cyberta on 06.12.17. * TODO: Mock AsyncTask */ -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PrepareForTest({ProfileManager.class}) public class EipStatusTest { EipStatus eipStatus; @@ -46,9 +52,28 @@ public class EipStatusTest { assertTrue("LEVEL_CONNECTED state", eipStatus.getState().equals("CONNECTED")); } - @Test(expected= IllegalStateException.class) - public void testUpdateState_LEVEL_VPNPAUSED() throws Exception { - VpnStatus.updateStateString("USERPAUSE", "", R.string.state_userpause, LEVEL_VPNPAUSED); + @Test + public void testUpdateState_LEVEL_VPNPAUSED_hasPersistentTun() throws Exception { + + mockStatic(ProfileManager.class); + VpnProfile mockVpnProfile = new VpnProfile("mockProfile"); + mockVpnProfile.mPersistTun = true; + when(ProfileManager.getLastConnectedVpn()).thenReturn(mockVpnProfile); + VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED); + assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == CONNECTING); + assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED); + } + + @Test + public void testUpdateState_LEVEL_VPNPAUSED_hasNotPersistentTun() throws Exception { + + mockStatic(ProfileManager.class); + VpnProfile mockVpnProfile = new VpnProfile("mockProfile"); + mockVpnProfile.mPersistTun = false; + when(ProfileManager.getLastConnectedVpn()).thenReturn(mockVpnProfile); + VpnStatus.updateStateString("SCREENOFF", "", R.string.state_screenoff, LEVEL_VPNPAUSED); + assertTrue("LEVEL_VPN_PAUSED eipLevel", eipStatus.getEipLevel() == DISCONNECTED); + assertTrue("LEVEL_VPN_PAUSED level", eipStatus.getLevel() == LEVEL_VPNPAUSED); } @Test -- cgit v1.2.3