summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2024-01-24 21:59:27 +0100
committercyBerta <cyberta@riseup.net>2024-01-24 22:13:21 +0100
commit5db48094b7cbc4bab12e2fe8476ff1404971e49c (patch)
tree0ea54ecd998225cec19cbc5c9d9632de0daf5efd
parenta43be4240f48bca2985a4cc3ddee124ff80bfb1a (diff)
fix GatewaySelectorTest, introduce TimezoneHelper
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java18
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/TimezoneHelper.java42
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java6
-rw-r--r--app/src/test/java/android/text/TextUtils.java6
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java74
5 files changed, 86 insertions, 60 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java
index 9289738a..d416a1ab 100644
--- a/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/ConfigHelper.java
@@ -48,7 +48,6 @@ import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
-import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -206,27 +205,14 @@ public class ConfigHelper {
return BuildConfig.priotize_anonymous_usage;
}
- public static int getCurrentTimezone() {
- return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
- }
-
- public static int timezoneDistance(int local_timezone, int remoteTimezone) {
- // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12
- int dist = Math.abs(local_timezone - remoteTimezone);
- // Farther than 12 timezones and it's shorter around the "back"
- if (dist > 12)
- dist = 12 - (dist - 12); // Well i'll be. Absolute values make equations do funny things.
- return dist;
- }
-
/**
*
* @param remoteTimezone
* @return a value between 0.1 and 1.0
*/
public static double getConnectionQualityFromTimezoneDistance(int remoteTimezone) {
- int localTimeZone = ConfigHelper.getCurrentTimezone();
- int distance = ConfigHelper.timezoneDistance(localTimeZone, remoteTimezone);
+ int localTimeZone = TimezoneHelper.getCurrentTimezone();
+ int distance = TimezoneHelper.timezoneDistance(localTimeZone, remoteTimezone);
return Math.max(distance / 12.0, 0.1);
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/TimezoneHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/TimezoneHelper.java
new file mode 100644
index 00000000..c543368d
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/TimezoneHelper.java
@@ -0,0 +1,42 @@
+package se.leap.bitmaskclient.base.utils;
+
+import androidx.annotation.VisibleForTesting;
+
+import java.util.Calendar;
+
+public class TimezoneHelper {
+
+ public interface TimezoneInterface {
+ int getCurrentTimezone();
+ }
+
+ private static TimezoneInterface instance = new DefaultTimezoneHelper();
+
+ @VisibleForTesting
+ public TimezoneHelper(TimezoneInterface timezoneInterface) {
+ instance = timezoneInterface;
+ }
+
+ public static TimezoneInterface get() {
+ return instance;
+ }
+
+ public static int timezoneDistance(int localTimezone, int remoteTimezone) { // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12
+ int dist = Math.abs(localTimezone - remoteTimezone);
+ // Farther than 12 timezones and it's shorter around the "back"
+ if (dist > 12)
+ dist = 12 - (dist - 12); // Well i'll be. Absolute values make equations do funny things.
+ return dist;
+ }
+
+ public static int getCurrentTimezone() {
+ return get().getCurrentTimezone();
+ }
+
+ public static class DefaultTimezoneHelper implements TimezoneInterface {
+ @Override
+ public int getCurrentTimezone() {
+ return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
+ }
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
index ad95c823..6a0b4b08 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
@@ -1,5 +1,8 @@
package se.leap.bitmaskclient.eip;
+import static se.leap.bitmaskclient.base.utils.TimezoneHelper.timezoneDistance;
+import static se.leap.bitmaskclient.base.utils.TimezoneHelper.getCurrentTimezone;
+
import android.util.Log;
import java.util.ArrayList;
@@ -10,9 +13,6 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
-import static se.leap.bitmaskclient.base.utils.ConfigHelper.getCurrentTimezone;
-import static se.leap.bitmaskclient.base.utils.ConfigHelper.timezoneDistance;
-
public class GatewaySelector {
private final static String TAG = GatewaySelector.class.getSimpleName();
List<Gateway> gateways;
diff --git a/app/src/test/java/android/text/TextUtils.java b/app/src/test/java/android/text/TextUtils.java
new file mode 100644
index 00000000..520944a5
--- /dev/null
+++ b/app/src/test/java/android/text/TextUtils.java
@@ -0,0 +1,6 @@
+package android.text;
+public class TextUtils {
+ public static boolean isEmpty(CharSequence str) {
+ return str == null || str.length() == 0;
+ }
+}
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 74762813..0175745e 100644
--- a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaySelectorTest.java
@@ -1,8 +1,10 @@
package se.leap.bitmaskclient.eip;
-import android.preference.PreferenceManager;
-import android.text.TextUtils;
-import android.util.Log;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+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 com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
@@ -14,35 +16,22 @@ import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
import de.blinkt.openvpn.core.ConfigParser;
import se.leap.bitmaskclient.base.models.Provider;
-import se.leap.bitmaskclient.base.utils.ConfigHelper;
import se.leap.bitmaskclient.base.utils.PreferenceHelper;
+import se.leap.bitmaskclient.base.utils.TimezoneHelper;
import se.leap.bitmaskclient.testutils.MockSharedPreferences;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.powermock.api.mockito.PowerMockito.mockStatic;
-import static org.powermock.api.mockito.PowerMockito.when;
-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.MockHelper.mockTextUtils;
-import static se.leap.bitmaskclient.testutils.TestSetupHelper.getInputAsString;
-
/**
* Created by cyberta on 18.12.18.
*/
-@RunWith(PowerMockRunner.class)
-@PowerMockRunnerDelegate(DataProviderRunner.class)
-@PrepareForTest({ConfigHelper.class, TextUtils.class})
+@RunWith(DataProviderRunner.class)
public class GatewaySelectorTest {
public static final String TAG = GatewaySelectorTest.class.getSimpleName();
@@ -72,23 +61,26 @@ public class GatewaySelectorTest {
PreferenceHelper preferenceHelper;
- GatewaySelector gatewaySelector;
JSONObject eipDefinition;
- ArrayList<Gateway> gatewayList = new ArrayList<>();
-
@Before
- public void setup() throws IOException, JSONException, ConfigParser.ConfigParseError {
- mockStatic(ConfigHelper.class);
- when(ConfigHelper.timezoneDistance(anyInt(), anyInt())).thenCallRealMethod();
- mockTextUtils();
+ public void setup() throws IOException, JSONException {
preferenceHelper = new PreferenceHelper(new MockSharedPreferences());
eipDefinition = new JSONObject(getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-four-gateways.json")));
- JSONArray gateways = eipDefinition.getJSONArray("gateways");
- for (int i = 0; i < gateways.length(); i++) {
- JSONObject gw = gateways.getJSONObject(i);
- JSONObject secrets = secretsConfiguration();
- Gateway aux = new Gateway(eipDefinition, secrets, gw, null);
- gatewayList.add(aux);
+ }
+
+ private List<Gateway> getGateways() {
+ try {
+ Vector<Gateway> gatewayList = new Vector<>();
+ JSONArray gateways = eipDefinition.getJSONArray("gateways");
+ for (int i = 0; i < gateways.length(); i++) {
+ JSONObject gw = gateways.getJSONObject(i);
+ JSONObject secrets = secretsConfiguration();
+ Gateway aux = new Gateway(eipDefinition, secrets, gw, null);
+ gatewayList.add(aux);
+ }
+ return gatewayList;
+ } catch (JSONException | IOException | ConfigParser.ConfigParseError e) {
+ return new ArrayList<>();
}
}
@@ -149,24 +141,24 @@ public class GatewaySelectorTest {
@Test
@UseDataProvider("dataProviderTimezones")
public void testSelect(int timezone, String expected) {
- when(ConfigHelper.getCurrentTimezone()).thenReturn(timezone);
- gatewaySelector = new GatewaySelector(gatewayList);
+ TimezoneHelper timezoneHelper = new TimezoneHelper(() -> timezone);
+ GatewaySelector gatewaySelector = new GatewaySelector(getGateways());
assertEquals(expected, gatewaySelector.select().getName());
}
@Test
@UseDataProvider("dataProviderSameDistanceTimezones")
public void testSelectSameTimezoneDistance(int timezone, String expected1, String expected2) {
- when(ConfigHelper.getCurrentTimezone()).thenReturn(timezone);
- gatewaySelector = new GatewaySelector(gatewayList);
+ TimezoneHelper timezoneHelper = new TimezoneHelper(() -> timezone);
+ GatewaySelector gatewaySelector = new GatewaySelector(getGateways());
assertTrue(gatewaySelector.select().getName().equals(expected1) || gatewaySelector.select().getName().equals(expected2));
}
@Test
@UseDataProvider("dataProviderSameDistanceTimezones")
public void testNClostest_SameTimezoneDistance_chooseGatewayWithSameDistance(int timezone, String expected1, String expected2) {
- when(ConfigHelper.getCurrentTimezone()).thenReturn(timezone);
- gatewaySelector = new GatewaySelector(gatewayList);
+ TimezoneHelper timezoneHelper = new TimezoneHelper(() -> timezone);
+ GatewaySelector gatewaySelector = new GatewaySelector(getGateways());
ArrayList<String> gateways = new ArrayList<>();
gateways.add(gatewaySelector.select(0).getName());
gateways.add(gatewaySelector.select(1).getName());
@@ -177,8 +169,8 @@ public class GatewaySelectorTest {
@Test
public void testNClostest_OneTimezonePerSet_choseSecondClosestTimezone() {
- when(ConfigHelper.getCurrentTimezone()).thenReturn(-4);
- gatewaySelector = new GatewaySelector(gatewayList);
+ TimezoneHelper timezoneHelper = new TimezoneHelper(() -> -4);
+ GatewaySelector gatewaySelector = new GatewaySelector(getGateways());
assertTrue("Frankfurt".equals(gatewaySelector.select(1).getName()));
}