From 6b032b751324a30120cfaabe88940f95171df11f Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 29 Dec 2020 00:54:08 +0100 Subject: new year cleanup: restructure messy project --- .../se/leap/bitmaskclient/DefaultedURLTest.java | 31 ---- .../leap/bitmaskclient/PreferenceHelperTest.java | 52 ------ .../se/leap/bitmaskclient/ProviderManagerTest.java | 189 -------------------- .../java/se/leap/bitmaskclient/ProviderTest.java | 86 ---------- .../base/models/DefaultedURLTest.java | 33 ++++ .../bitmaskclient/base/models/ProviderTest.java | 87 ++++++++++ .../base/utils/PreferenceHelperTest.java | 53 ++++++ .../se/leap/bitmaskclient/eip/EipStatusTest.java | 7 +- .../bitmaskclient/eip/GatewaySelectorTest.java | 8 +- .../bitmaskclient/eip/GatewaysManagerTest.java | 19 +- .../bitmaskclient/eip/ProviderApiManagerTest.java | 35 ++-- .../providersetup/ProviderManagerTest.java | 191 +++++++++++++++++++++ .../BackendMockResponses/BaseBackendResponse.java | 2 +- .../leap/bitmaskclient/testutils/MockHelper.java | 24 +-- .../tethering/TetheringStateManagerTest.java | 2 +- 15 files changed, 409 insertions(+), 410 deletions(-) delete mode 100644 app/src/test/java/se/leap/bitmaskclient/DefaultedURLTest.java delete mode 100644 app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java delete mode 100644 app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java delete mode 100644 app/src/test/java/se/leap/bitmaskclient/ProviderTest.java create mode 100644 app/src/test/java/se/leap/bitmaskclient/base/models/DefaultedURLTest.java create mode 100644 app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java create mode 100644 app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java create mode 100644 app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java (limited to 'app/src/test/java') diff --git a/app/src/test/java/se/leap/bitmaskclient/DefaultedURLTest.java b/app/src/test/java/se/leap/bitmaskclient/DefaultedURLTest.java deleted file mode 100644 index cbf47621..00000000 --- a/app/src/test/java/se/leap/bitmaskclient/DefaultedURLTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package se.leap.bitmaskclient; - -import org.junit.Test; - -import java.net.MalformedURLException; -import java.net.URL; - -import static org.junit.Assert.*; - -/** - * Created by cyberta on 11.02.18. - */ -public class DefaultedURLTest { - - @Test - public void testEquals_false() throws MalformedURLException { - DefaultedURL defaultedURL = new DefaultedURL(); - DefaultedURL customURL = new DefaultedURL(); - customURL.setUrl(new URL("https://customurl.com")); - - assertFalse(defaultedURL.equals(customURL)); - } - - @Test - public void testEquals_true() throws MalformedURLException { - DefaultedURL defaultedURL = new DefaultedURL(); - DefaultedURL customURL = new DefaultedURL(); - assertTrue(defaultedURL.equals(customURL)); - } - -} diff --git a/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java deleted file mode 100644 index edac3480..00000000 --- a/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package se.leap.bitmaskclient; - -import android.content.SharedPreferences; - -import org.junit.Before; -import org.junit.Test; - -import se.leap.bitmaskclient.testutils.MockSharedPreferences; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; -import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; -import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences; - -/** - * Created by cyberta on 17.01.18. - */ -public class PreferenceHelperTest { - - private SharedPreferences mockPreferences; - - - @Before - public void setup() { - mockPreferences = new MockSharedPreferences(); - } - - @Test - public void getSavedProviderFromSharedPreferences_notInPreferences_returnsDefaultProvider() throws Exception { - Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); - assertFalse(provider.isConfigured()); - } - - @Test - public void getSavedProviderFromSharedPreferences_notInPreferences_returnsConfiguredProvider() throws Exception { - mockPreferences.edit() - .putString(Provider.KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.json"))) - .putString(Provider.MAIN_URL, "https://riseup.net") - .putString(Provider.CA_CERT, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.pem"))) - .putString(PROVIDER_EIP_DEFINITION, getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json"))) - .putString(PROVIDER_VPN_CERTIFICATE, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.vpn_cert.pem"))) - .putString(PROVIDER_PRIVATE_KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("private_rsa_key.pem"))) - .apply(); - Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); - assertTrue(provider.isConfigured()); - } - - -} \ No newline at end of file diff --git a/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java deleted file mode 100644 index 1914f989..00000000 --- a/app/src/test/java/se/leap/bitmaskclient/ProviderManagerTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package se.leap.bitmaskclient; - -import android.content.res.AssetManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.io.File; -import java.io.InputStream; -import java.util.ArrayList; - -import se.leap.bitmaskclient.utils.ConfigHelper; -import se.leap.bitmaskclient.utils.FileHelper; -import se.leap.bitmaskclient.utils.InputStreamHelper; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.verifyStatic; -import static se.leap.bitmaskclient.testutils.MockHelper.mockFileHelper; -import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper; - -/** - * Created by cyberta on 20.02.18. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({ConfigHelper.class, FileHelper.class, InputStreamHelper.class}) -public class ProviderManagerTest { - - @Mock - private AssetManager assetManager; - @Mock - private File file; - private ProviderManager providerManager; - - @Before - public void setup() throws Exception { - //mock assetManager methods - //-------------------------- - when(assetManager.open(anyString())).thenAnswer(new Answer() { - @Override - public InputStream answer(InvocationOnMock invocation) throws Throwable { - String filename = "preconfigured/" + invocation.getArguments()[0]; - return getClass().getClassLoader().getResourceAsStream(filename); - } - }); - when(assetManager.list(anyString())).thenAnswer(new Answer() { - @Override - public String[] answer(InvocationOnMock invocation) throws Throwable { - String path = (String) invocation.getArguments()[0]; - if ("urls".equals(path)) { - String[] preconfiguredUrls = new String[3]; - preconfiguredUrls[0] = "calyx.net.url"; - preconfiguredUrls[1] = "demo.bitmask.net.url"; - preconfiguredUrls[2] = "riseup.net.url"; - return preconfiguredUrls; - } else - throw new IllegalArgumentException("You need to implement the expected path manually!"); - } - }); - - //mock File methods - //------------------ - when(file.isDirectory()).thenReturn(true); - - ArrayList mockedCustomProviderList = new ArrayList<>(); - mockedCustomProviderList.add("leapcolombia.json"); - String[] mockedCustomProviderArray = new String[mockedCustomProviderList.size()]; - mockedCustomProviderArray = mockedCustomProviderList.toArray(mockedCustomProviderArray); - when(file.list()).thenReturn(mockedCustomProviderArray); - - when(file.getAbsolutePath()).thenReturn("externalDir"); - when(file.getPath()).thenReturn("externalDir"); - mockFileHelper(file); - - // mock inputStream - //----------------------------------- - mockInputStreamHelper(); - - } - - @After - public void tearDown() { - ProviderManager.reset(); - } - - @Test - public void testSize_has5ProvidersWithCurrentTestSetup() { - providerManager = ProviderManager.getInstance(assetManager, file); - assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size()); - } - - @Test - public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://anewprovider.org"); - assertTrue("custom provider added: ", providerManager.add(customProvider)); - assertEquals("3 preconfigured, 2 custom providers, 1 dummy provider", 6, providerManager.providers().size()); - } - - @Test - public void testAdd_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://leapcolombia.org"); - assertFalse("custom provider added: ", providerManager.add(customProvider)); - assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); - } - - @Test - public void testAdd_newCustomProviderThatIsPartOfDefaultButNotOfCustomList_returnFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://demo.bitmask.net"); - assertFalse("custom provider added: ", providerManager.add(customProvider)); - assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); - } - - @Test - public void testRemove_ProviderIsPartOfDefaultButNotCustomList_returnsFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://demo.bitmask.net"); - assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); - assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); - } - - @Test - public void testRemove_ProviderIsNotPartOfDefaultButOfCustomList_returnsTrue() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://leapcolombia.org"); - assertTrue("custom provider not removed: ", providerManager.remove(customProvider)); - assertEquals("3 preconfigured, 0 custom providers, 1 dummy provider", 4, providerManager.providers().size()); - } - - @Test - public void testRemove_ProviderIsNotPartOfDefaultNorOfCustomList_returnsFalse() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://anotherprovider.org"); - assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); - assertEquals("3 preconfigured, 1 custom providers, 1 dummy provider", 5, providerManager.providers().size()); - } - - @Test - public void testClear_ProvidersListHasOnlyDummyProvider() throws Exception { - providerManager = ProviderManager.getInstance(assetManager, file); - providerManager.clear(); - assertEquals("1 providers", 1, providerManager.providers().size()); - assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrlString()); - } - - @Test - public void testSaveCustomProvidersToFile_CustomProviderDeleted_deletesFromDir() throws Exception { - when(file.exists()).thenReturn(true); - providerManager = ProviderManager.getInstance(assetManager, file); - //leapcolombia is mocked custom provider from setup - Provider customProvider = new Provider("https://leapcolombia.org"); - providerManager.remove(customProvider); - providerManager.saveCustomProvidersToFile(); - verify(file, times(1)).delete(); - } - - - @Test - public void testSaveCustomProvidersToFile_newCustomProviders_persistNew() throws Exception { - when(file.list()).thenReturn(new String[0]); - when(file.exists()).thenReturn(false); - providerManager = ProviderManager.getInstance(assetManager, file); - Provider customProvider = new Provider("https://anotherprovider.org"); - Provider secondCustomProvider = new Provider("https://yetanotherprovider.org"); - providerManager.add(customProvider); - providerManager.add(secondCustomProvider); - providerManager.saveCustomProvidersToFile(); - - verifyStatic(FileHelper.class, times(2)); - FileHelper.persistFile(any(File.class), anyString()); - } - - -} \ No newline at end of file diff --git a/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java b/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java deleted file mode 100644 index 8a74e5de..00000000 --- a/app/src/test/java/se/leap/bitmaskclient/ProviderTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package se.leap.bitmaskclient; - -import org.junit.Test; - -import java.util.HashSet; -import java.util.Set; - -import se.leap.bitmaskclient.testutils.TestSetupHelper; - -import static junit.framework.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Created by cyberta on 12.02.18. - */ -public class ProviderTest { - - @Test - public void testEquals_sameFields_returnsTrue() throws Exception { - Provider p1 = TestSetupHelper.getConfiguredProvider(); - Provider p2 = TestSetupHelper.getConfiguredProvider(); - assertTrue("Providers should be same:", p1.equals(p2)); - } - - @Test - public void testEquals_differntMainUrl_returnsFalse() throws Exception { - Provider p1 = TestSetupHelper.getConfiguredProvider(); - Provider p2 = TestSetupHelper.getConfiguredProvider(); - p2.setMainUrl("http://somethingsdiffer.org"); - assertFalse("Providers should be same:", p1.equals(p2)); - } - - @Test - public void testEquals_differentGeoIpUrl_returnsFalse() throws Exception { - Provider p1 = TestSetupHelper.getConfiguredProvider(); - Provider p2 = TestSetupHelper.getConfiguredProvider(); - p2.setGeoipUrl(null); - assertFalse("Providers should be same:", p1.equals(p2)); - } - - // see ProviderManagerTest testing add(...) - @Test - public void testEqualsThroughSetContains_differentFields_returnsFalse() throws Exception { - Provider p1 = TestSetupHelper.getConfiguredProvider(); - Provider p2 = TestSetupHelper.getConfiguredProvider(); - p2.setMainUrl("http://somethingsdiffer.org"); - Provider p3 = new Provider("https://anotherprovider.net"); - - Set defaultProviders = new HashSet<>(); - defaultProviders.add(p1); - defaultProviders.add(p2); - - assertTrue(defaultProviders.contains(p1)); - assertTrue(defaultProviders.contains(p2)); - assertFalse(defaultProviders.contains(p3)); - } - - @Test - public void testIsPluggableTransportsSupported_Obfs4_returnsTrue() throws Exception { - Provider p1 = TestSetupHelper.getProvider( - "https://pt.demo.bitmask.net", - null, - null, - null, - null, - null, - "ptdemo.bitmask.eip-service.json", - null); - assertTrue(p1.supportsPluggableTransports()); - } - - @Test - public void testIsPluggableTransportsSupported_noObfs4_returnsFalse() throws Exception { - Provider p1 = TestSetupHelper.getProvider( - null, - null, - null, - null, - null, - null, - "eip-service-two-gateways.json", - null); - assertFalse(p1.supportsPluggableTransports()); - } - -} diff --git a/app/src/test/java/se/leap/bitmaskclient/base/models/DefaultedURLTest.java b/app/src/test/java/se/leap/bitmaskclient/base/models/DefaultedURLTest.java new file mode 100644 index 00000000..551206d9 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/base/models/DefaultedURLTest.java @@ -0,0 +1,33 @@ +package se.leap.bitmaskclient.base.models; + +import org.junit.Test; + +import java.net.MalformedURLException; +import java.net.URL; + +import se.leap.bitmaskclient.base.models.DefaultedURL; + +import static org.junit.Assert.*; + +/** + * Created by cyberta on 11.02.18. + */ +public class DefaultedURLTest { + + @Test + public void testEquals_false() throws MalformedURLException { + DefaultedURL defaultedURL = new DefaultedURL(); + DefaultedURL customURL = new DefaultedURL(); + customURL.setUrl(new URL("https://customurl.com")); + + assertFalse(defaultedURL.equals(customURL)); + } + + @Test + public void testEquals_true() throws MalformedURLException { + DefaultedURL defaultedURL = new DefaultedURL(); + DefaultedURL customURL = new DefaultedURL(); + assertTrue(defaultedURL.equals(customURL)); + } + +} diff --git a/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java b/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java new file mode 100644 index 00000000..aaf3f255 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/base/models/ProviderTest.java @@ -0,0 +1,87 @@ +package se.leap.bitmaskclient.base.models; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.testutils.TestSetupHelper; + +import static junit.framework.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by cyberta on 12.02.18. + */ +public class ProviderTest { + + @Test + public void testEquals_sameFields_returnsTrue() throws Exception { + Provider p1 = TestSetupHelper.getConfiguredProvider(); + Provider p2 = TestSetupHelper.getConfiguredProvider(); + assertTrue("Providers should be same:", p1.equals(p2)); + } + + @Test + public void testEquals_differntMainUrl_returnsFalse() throws Exception { + Provider p1 = TestSetupHelper.getConfiguredProvider(); + Provider p2 = TestSetupHelper.getConfiguredProvider(); + p2.setMainUrl("http://somethingsdiffer.org"); + assertFalse("Providers should be same:", p1.equals(p2)); + } + + @Test + public void testEquals_differentGeoIpUrl_returnsFalse() throws Exception { + Provider p1 = TestSetupHelper.getConfiguredProvider(); + Provider p2 = TestSetupHelper.getConfiguredProvider(); + p2.setGeoipUrl(null); + assertFalse("Providers should be same:", p1.equals(p2)); + } + + // see ProviderManagerTest testing add(...) + @Test + public void testEqualsThroughSetContains_differentFields_returnsFalse() throws Exception { + Provider p1 = TestSetupHelper.getConfiguredProvider(); + Provider p2 = TestSetupHelper.getConfiguredProvider(); + p2.setMainUrl("http://somethingsdiffer.org"); + Provider p3 = new Provider("https://anotherprovider.net"); + + Set defaultProviders = new HashSet<>(); + defaultProviders.add(p1); + defaultProviders.add(p2); + + assertTrue(defaultProviders.contains(p1)); + assertTrue(defaultProviders.contains(p2)); + assertFalse(defaultProviders.contains(p3)); + } + + @Test + public void testIsPluggableTransportsSupported_Obfs4_returnsTrue() throws Exception { + Provider p1 = TestSetupHelper.getProvider( + "https://pt.demo.bitmask.net", + null, + null, + null, + null, + null, + "ptdemo.bitmask.eip-service.json", + null); + assertTrue(p1.supportsPluggableTransports()); + } + + @Test + public void testIsPluggableTransportsSupported_noObfs4_returnsFalse() throws Exception { + Provider p1 = TestSetupHelper.getProvider( + null, + null, + null, + null, + null, + null, + "eip-service-two-gateways.json", + null); + assertFalse(p1.supportsPluggableTransports()); + } + +} diff --git a/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java new file mode 100644 index 00000000..955ddc44 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/base/utils/PreferenceHelperTest.java @@ -0,0 +1,53 @@ +package se.leap.bitmaskclient.base.utils; + +import android.content.SharedPreferences; + +import org.junit.Before; +import org.junit.Test; + +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.testutils.MockSharedPreferences; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getSavedProviderFromSharedPreferences; + +/** + * Created by cyberta on 17.01.18. + */ +public class PreferenceHelperTest { + + private SharedPreferences mockPreferences; + + + @Before + public void setup() { + mockPreferences = new MockSharedPreferences(); + } + + @Test + public void getSavedProviderFromSharedPreferences_notInPreferences_returnsDefaultProvider() throws Exception { + Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); + assertFalse(provider.isConfigured()); + } + + @Test + public void getSavedProviderFromSharedPreferences_notInPreferences_returnsConfiguredProvider() throws Exception { + mockPreferences.edit() + .putString(Provider.KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.json"))) + .putString(Provider.MAIN_URL, "https://riseup.net") + .putString(Provider.CA_CERT, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.pem"))) + .putString(PROVIDER_EIP_DEFINITION, getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json"))) + .putString(PROVIDER_VPN_CERTIFICATE, getInputAsString(getClass().getClassLoader().getResourceAsStream("riseup.net.vpn_cert.pem"))) + .putString(PROVIDER_PRIVATE_KEY, getInputAsString(getClass().getClassLoader().getResourceAsStream("private_rsa_key.pem"))) + .apply(); + Provider provider = getSavedProviderFromSharedPreferences(mockPreferences); + assertTrue(provider.isConfigured()); + } + + +} \ No newline at end of file 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 8ba7f5fc..1e15c5ab 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/EipStatusTest.java @@ -3,18 +3,14 @@ package se.leap.bitmaskclient.eip; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatchers; 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.VpnStatus; -import de.blinkt.openvpn.core.connection.Connection; import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.testutils.MockHelper; -import se.leap.bitmaskclient.testutils.TestSetupHelper; -import se.leap.bitmaskclient.utils.PreferenceHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_AUTH_FAILED; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_CONNECTED; @@ -26,7 +22,6 @@ import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_WAITING_FOR_USER_INP import static de.blinkt.openvpn.core.ConnectionStatus.UNKNOWN_LEVEL; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; import static junit.framework.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.anyString; import static org.powermock.api.mockito.PowerMockito.doNothing; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java index 5d5a959b..8c074dd8 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java @@ -18,15 +18,15 @@ import java.io.IOException; import java.util.ArrayList; import de.blinkt.openvpn.core.ConfigParser; -import se.leap.bitmaskclient.Provider; -import se.leap.bitmaskclient.utils.ConfigHelper; +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.utils.ConfigHelper; import static junit.framework.Assert.assertEquals; 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.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString; /** diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java index bfe96d2c..01e27320 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java @@ -18,14 +18,13 @@ import java.io.IOException; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; -import de.blinkt.openvpn.core.connection.Connection; -import se.leap.bitmaskclient.Provider; -import se.leap.bitmaskclient.ProviderObservable; +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.models.ProviderObservable; import se.leap.bitmaskclient.testutils.MockHelper; import se.leap.bitmaskclient.testutils.MockSharedPreferences; import se.leap.bitmaskclient.testutils.TestSetupHelper; -import se.leap.bitmaskclient.utils.ConfigHelper; -import se.leap.bitmaskclient.utils.PreferenceHelper; +import se.leap.bitmaskclient.base.utils.ConfigHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4; import static de.blinkt.openvpn.core.connection.Connection.TransportType.OPENVPN; @@ -38,11 +37,11 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static se.leap.bitmaskclient.Constants.GATEWAYS; -import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; -import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.Provider.CA_CERT; +import static se.leap.bitmaskclient.base.models.Constants.GATEWAYS; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.base.models.Provider.CA_CERT; import static se.leap.bitmaskclient.testutils.TestSetupHelper.getProvider; /** diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java index efa6f78c..38449c20 100644 --- a/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/eip/ProviderApiManagerTest.java @@ -40,25 +40,24 @@ import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateEncodingException; import se.leap.bitmaskclient.BuildConfig; -import se.leap.bitmaskclient.Provider; -import se.leap.bitmaskclient.ProviderAPI; -import se.leap.bitmaskclient.ProviderApiConnector; -import se.leap.bitmaskclient.ProviderApiManager; -import se.leap.bitmaskclient.ProviderApiManagerBase; -import se.leap.bitmaskclient.testutils.BackendMockResponses.GeoIpServiceIsDownBackendResponse; +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.providersetup.ProviderAPI; +import se.leap.bitmaskclient.providersetup.ProviderApiConnector; +import se.leap.bitmaskclient.providersetup.ProviderApiManager; +import se.leap.bitmaskclient.providersetup.ProviderApiManagerBase; import se.leap.bitmaskclient.testutils.MockSharedPreferences; -import se.leap.bitmaskclient.utils.ConfigHelper; -import se.leap.bitmaskclient.utils.PreferenceHelper; - -import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; -import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; -import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; -import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_DOWNLOADED_GEOIP_JSON; -import static se.leap.bitmaskclient.ProviderAPI.ERRORS; -import static se.leap.bitmaskclient.ProviderAPI.INCORRECTLY_DOWNLOADED_GEOIP_JSON; -import static se.leap.bitmaskclient.ProviderAPI.PARAMETERS; -import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_NOK; -import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_OK; +import se.leap.bitmaskclient.base.utils.ConfigHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; + +import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_KEY; +import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.CORRECTLY_DOWNLOADED_GEOIP_JSON; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_GEOIP_JSON; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.PARAMETERS; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.PROVIDER_NOK; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.PROVIDER_OK; import static se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider.TestBackendErrorCase.ERROR_CASE_FETCH_EIP_SERVICE_CERTIFICATE_INVALID; import static se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider.TestBackendErrorCase.ERROR_CASE_MICONFIGURED_PROVIDER; import static se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider.TestBackendErrorCase.ERROR_CASE_UPDATED_CERTIFICATE; diff --git a/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java new file mode 100644 index 00000000..c48f520e --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java @@ -0,0 +1,191 @@ +package se.leap.bitmaskclient.providersetup; + +import android.content.res.AssetManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.File; +import java.io.InputStream; +import java.util.ArrayList; + +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.utils.ConfigHelper; +import se.leap.bitmaskclient.base.utils.FileHelper; +import se.leap.bitmaskclient.base.utils.InputStreamHelper; +import se.leap.bitmaskclient.providersetup.ProviderManager; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static se.leap.bitmaskclient.testutils.MockHelper.mockFileHelper; +import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper; + +/** + * Created by cyberta on 20.02.18. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ConfigHelper.class, FileHelper.class, InputStreamHelper.class}) +public class ProviderManagerTest { + + @Mock + private AssetManager assetManager; + @Mock + private File file; + private ProviderManager providerManager; + + @Before + public void setup() throws Exception { + //mock assetManager methods + //-------------------------- + when(assetManager.open(anyString())).thenAnswer(new Answer() { + @Override + public InputStream answer(InvocationOnMock invocation) throws Throwable { + String filename = "preconfigured/" + invocation.getArguments()[0]; + return getClass().getClassLoader().getResourceAsStream(filename); + } + }); + when(assetManager.list(anyString())).thenAnswer(new Answer() { + @Override + public String[] answer(InvocationOnMock invocation) throws Throwable { + String path = (String) invocation.getArguments()[0]; + if ("urls".equals(path)) { + String[] preconfiguredUrls = new String[3]; + preconfiguredUrls[0] = "calyx.net.url"; + preconfiguredUrls[1] = "demo.bitmask.net.url"; + preconfiguredUrls[2] = "riseup.net.url"; + return preconfiguredUrls; + } else + throw new IllegalArgumentException("You need to implement the expected path manually!"); + } + }); + + //mock File methods + //------------------ + when(file.isDirectory()).thenReturn(true); + + ArrayList mockedCustomProviderList = new ArrayList<>(); + mockedCustomProviderList.add("leapcolombia.json"); + String[] mockedCustomProviderArray = new String[mockedCustomProviderList.size()]; + mockedCustomProviderArray = mockedCustomProviderList.toArray(mockedCustomProviderArray); + when(file.list()).thenReturn(mockedCustomProviderArray); + + when(file.getAbsolutePath()).thenReturn("externalDir"); + when(file.getPath()).thenReturn("externalDir"); + mockFileHelper(file); + + // mock inputStream + //----------------------------------- + mockInputStreamHelper(); + + } + + @After + public void tearDown() { + ProviderManager.reset(); + } + + @Test + public void testSize_has5ProvidersWithCurrentTestSetup() { + providerManager = ProviderManager.getInstance(assetManager, file); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size()); + } + + @Test + public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://anewprovider.org"); + assertTrue("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 2 custom providers, 1 dummy provider", 6, providerManager.providers().size()); + } + + @Test + public void testAdd_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://leapcolombia.org"); + assertFalse("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testAdd_newCustomProviderThatIsPartOfDefaultButNotOfCustomList_returnFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://demo.bitmask.net"); + assertFalse("custom provider added: ", providerManager.add(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsPartOfDefaultButNotCustomList_returnsFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://demo.bitmask.net"); + assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsNotPartOfDefaultButOfCustomList_returnsTrue() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://leapcolombia.org"); + assertTrue("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 0 custom providers, 1 dummy provider", 4, providerManager.providers().size()); + } + + @Test + public void testRemove_ProviderIsNotPartOfDefaultNorOfCustomList_returnsFalse() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://anotherprovider.org"); + assertFalse("custom provider not removed: ", providerManager.remove(customProvider)); + assertEquals("3 preconfigured, 1 custom providers, 1 dummy provider", 5, providerManager.providers().size()); + } + + @Test + public void testClear_ProvidersListHasOnlyDummyProvider() throws Exception { + providerManager = ProviderManager.getInstance(assetManager, file); + providerManager.clear(); + assertEquals("1 providers", 1, providerManager.providers().size()); + assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrlString()); + } + + @Test + public void testSaveCustomProvidersToFile_CustomProviderDeleted_deletesFromDir() throws Exception { + when(file.exists()).thenReturn(true); + providerManager = ProviderManager.getInstance(assetManager, file); + //leapcolombia is mocked custom provider from setup + Provider customProvider = new Provider("https://leapcolombia.org"); + providerManager.remove(customProvider); + providerManager.saveCustomProvidersToFile(); + verify(file, times(1)).delete(); + } + + + @Test + public void testSaveCustomProvidersToFile_newCustomProviders_persistNew() throws Exception { + when(file.list()).thenReturn(new String[0]); + when(file.exists()).thenReturn(false); + providerManager = ProviderManager.getInstance(assetManager, file); + Provider customProvider = new Provider("https://anotherprovider.org"); + Provider secondCustomProvider = new Provider("https://yetanotherprovider.org"); + providerManager.add(customProvider); + providerManager.add(secondCustomProvider); + providerManager.saveCustomProvidersToFile(); + + verifyStatic(FileHelper.class, times(2)); + FileHelper.persistFile(any(File.class), anyString()); + } + + +} \ No newline at end of file diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/BackendMockResponses/BaseBackendResponse.java b/app/src/test/java/se/leap/bitmaskclient/testutils/BackendMockResponses/BaseBackendResponse.java index 98224019..9a538d3c 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/BackendMockResponses/BaseBackendResponse.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/BackendMockResponses/BaseBackendResponse.java @@ -25,7 +25,7 @@ import org.mockito.stubbing.Answer; import java.io.IOException; import okhttp3.OkHttpClient; -import se.leap.bitmaskclient.ProviderApiConnector; +import se.leap.bitmaskclient.providersetup.ProviderApiConnector; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; diff --git a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java index d3d07308..d4b7c5d1 100644 --- a/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java +++ b/app/src/test/java/se/leap/bitmaskclient/testutils/MockHelper.java @@ -34,16 +34,16 @@ import java.util.Map; import java.util.Set; import okhttp3.OkHttpClient; -import se.leap.bitmaskclient.OkHttpClientGenerator; -import se.leap.bitmaskclient.Provider; -import se.leap.bitmaskclient.ProviderObservable; +import se.leap.bitmaskclient.providersetup.connectivity.OkHttpClientGenerator; +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.models.ProviderObservable; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.testutils.BackendMockResponses.BackendMockProvider; import se.leap.bitmaskclient.testutils.matchers.BundleMatcher; -import se.leap.bitmaskclient.utils.ConfigHelper; -import se.leap.bitmaskclient.utils.FileHelper; -import se.leap.bitmaskclient.utils.InputStreamHelper; -import se.leap.bitmaskclient.utils.PreferenceHelper; +import se.leap.bitmaskclient.base.utils.ConfigHelper; +import se.leap.bitmaskclient.base.utils.FileHelper; +import se.leap.bitmaskclient.base.utils.InputStreamHelper; +import se.leap.bitmaskclient.base.utils.PreferenceHelper; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -57,11 +57,11 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.utils.FileHelper.createFile; -import static se.leap.bitmaskclient.utils.PreferenceHelper.getEipDefinitionFromPreferences; -import static se.leap.bitmaskclient.utils.PreferenceHelper.getFromPersistedProvider; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.base.utils.FileHelper.createFile; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getEipDefinitionFromPreferences; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getFromPersistedProvider; /** * Created by cyberta on 29.01.18. 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 b3ab75ba..509805d4 100644 --- a/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java +++ b/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java @@ -36,7 +36,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; -import se.leap.bitmaskclient.utils.Cmd; +import se.leap.bitmaskclient.base.utils.Cmd; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; -- cgit v1.2.3