From 3c130ce5f2741d4bc1c521507ab118a3006ec634 Mon Sep 17 00:00:00 2001 From: cyberta Date: Thu, 30 Jan 2020 22:48:35 -0600 Subject: adapt and extend tests --- .../tethering/TetheringStateManagerTest.java | 51 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java index 85352c53..2e6292a6 100644 --- a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java @@ -93,7 +93,7 @@ public class TetheringStateManagerTest { } }); - TetheringObservable.setUsbTethering(false); + TetheringObservable.setUsbTethering(false, "192.168.42.0/24", "rndis0"); TetheringStateManager.getInstance().init(mockContext); TetheringStateManager manager = TetheringStateManager.getInstance(); assertTrue(observable.isUsbTetheringEnabled()); @@ -124,7 +124,7 @@ public class TetheringStateManagerTest { } }); - TetheringObservable.setUsbTethering(true); + TetheringObservable.setUsbTethering(true, "192.168.42.0/24", "rndis0"); TetheringStateManager.getInstance().init(mockContext); TetheringStateManager manager = TetheringStateManager.getInstance(); assertFalse(observable.isUsbTetheringEnabled()); @@ -139,7 +139,7 @@ public class TetheringStateManagerTest { PowerMockito.mockStatic(NetworkInterface.class); PowerMockito.when(NetworkInterface.getNetworkInterfaces()).thenThrow(new SocketException()); - TetheringObservable.setUsbTethering(true); + TetheringObservable.setUsbTethering(true, "192.168.42.0/24", "rndis0"); TetheringStateManager.getInstance().init(mockContext); TetheringStateManager manager = TetheringStateManager.getInstance(); assertFalse(observable.isUsbTetheringEnabled()); @@ -277,12 +277,45 @@ public class TetheringStateManagerTest { } @Test - public void testGetWifiAddressRange() { - mockStatic(TetheringStateManager.class); - PowerMockito.when(TetheringStateManager.getWifiInterfaceAddress()).thenReturn("192.168.40.217/24"); - PowerMockito.when(TetheringStateManager.getInstance()).thenCallRealMethod(); - PowerMockito.when(TetheringStateManager.getWifiAddressRange()).thenCallRealMethod(); - assertEquals("192.168.40.0/24", TetheringStateManager.getWifiAddressRange()); + public void testGetWifiAddressRangee_keepsLastSeenAddressAndInterface() throws Exception { + WifiManagerWrapper mockWrapper = mock(WifiManagerWrapper.class); + when(mockWrapper.isWifiAPEnabled()).thenReturn(true); + PowerMockito.whenNew(WifiManagerWrapper.class).withAnyArguments().thenReturn(mockWrapper); + + //WifiTethering was switched on + TetheringObservable.setWifiTethering(true, "192.168.40.0/24", "wlan0"); + + assertEquals("192.168.40.0/24", observable.getTetheringState().wifiAddress); + assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenWifiAddress); + assertEquals("wlan0", observable.getTetheringState().wifiInterface); + assertEquals("wlan0", observable.getTetheringState().lastSeenWifiInterface); + //Wifi tethering was switched off + TetheringObservable.setWifiTethering(true, "", ""); + assertEquals("", observable.getTetheringState().wifiAddress); + assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenWifiAddress); + assertEquals("", observable.getTetheringState().wifiInterface); + assertEquals("wlan0", observable.getTetheringState().lastSeenWifiInterface); + } + + @Test + public void testGetUsbAddressRange_keepsLastSeenAddressAndInterface() throws Exception { + WifiManagerWrapper mockWrapper = mock(WifiManagerWrapper.class); + when(mockWrapper.isWifiAPEnabled()).thenReturn(true); + PowerMockito.whenNew(WifiManagerWrapper.class).withAnyArguments().thenReturn(mockWrapper); + + //WifiTethering was switched on + TetheringObservable.setWifiTethering(true, "192.168.40.0/24", "rndis0"); + + assertEquals("192.168.40.0/24", observable.getTetheringState().usbAddress); + assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenUsbAddress); + assertEquals("rndis0", observable.getTetheringState().usbInterface); + assertEquals("rndis0", observable.getTetheringState().lastSeenUsbAddress); + //Wifi tethering was switched off + TetheringObservable.setWifiTethering(true, "", ""); + assertEquals("", observable.getTetheringState().usbAddress); + assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenUsbAddress); + assertEquals("", observable.getTetheringState().usbInterface); } + } \ No newline at end of file -- cgit v1.2.3