summaryrefslogtreecommitdiff
path: root/app/src/test/java/se
diff options
context:
space:
mode:
authorcyberta <cyberta@riseup.net>2017-10-13 11:34:42 +0000
committercyberta <cyberta@riseup.net>2017-10-13 11:34:42 +0000
commitef91d9235cccbd4dcd9e120c8fff910e0d448022 (patch)
tree88b00bcb87c116190e45f1045c22952fb0ee4991 /app/src/test/java/se
parentef98b18734c25da77abe4f0067635cf795218c65 (diff)
parent97ff21c74619269a8c7dc9d8ff1485200db58d8c (diff)
Merge branch '8758_switch_providers' into 'master'
#8758 fix switching providers See merge request leap/bitmask_android!11
Diffstat (limited to 'app/src/test/java/se')
-rw-r--r--app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
new file mode 100644
index 00000000..e6a67392
--- /dev/null
+++ b/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
@@ -0,0 +1,84 @@
+package se.leap.bitmaskclient.eip;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.IOException;
+
+import se.leap.bitmaskclient.Provider;
+import se.leap.bitmaskclient.TestUtils;
+
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+/**
+ * Created by cyberta on 09.10.17.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class GatewaysManagerTest {
+
+ private GatewaysManager gatewaysManager;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private Context mockContext;
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private SharedPreferences sharedPreferences;
+
+ @Before
+ public void setUp() throws IOException, JSONException {
+
+
+ JSONObject secrets = new JSONObject(getJsonStringFor("secrets.json"));
+
+ when(sharedPreferences.getString(eq(Constants.PRIVATE_KEY), anyString())).thenReturn(secrets.getString(Constants.PRIVATE_KEY));
+ when(sharedPreferences.getString(eq(Provider.CA_CERT), anyString())).thenReturn(secrets.getString(Provider.CA_CERT));
+ when(sharedPreferences.getString(eq(Constants.CERTIFICATE), anyString())).thenReturn(secrets.getString(Constants.CERTIFICATE));
+ when(mockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPreferences);
+
+
+ gatewaysManager = new GatewaysManager(mockContext, sharedPreferences);
+ }
+
+ @Test
+ public void testFromEipServiceJson_emptyJson() throws Exception {
+ gatewaysManager.fromEipServiceJson(new JSONObject());
+ assertEquals(0, gatewaysManager.size());
+ }
+
+
+ @Test
+ public void testFromEipServiceJson_ignoreDuplicateGateways() throws Exception {
+ String eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json"));
+ gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson));
+ assertEquals(2, gatewaysManager.size());
+ eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-one-gateway.json"));
+ gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson));
+ assertEquals(2, gatewaysManager.size());
+ }
+
+ @Test
+ public void testClearGatewaysAndProfiles_resetGateways() throws Exception {
+ String eipServiceJson = TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream("eip-service-two-gateways.json"));
+ gatewaysManager.fromEipServiceJson(new JSONObject(eipServiceJson));
+ assertEquals(2, gatewaysManager.size());
+ gatewaysManager.clearGatewaysAndProfiles();
+ assertEquals(0, gatewaysManager.size());
+ }
+
+ private String getJsonStringFor(String filename) throws IOException {
+ return TestUtils.getInputAsString(getClass().getClassLoader().getResourceAsStream(filename));
+ }
+
+} \ No newline at end of file