diff options
author | cyBerta <cyberta@riseup.net> | 2017-10-09 15:22:23 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2017-10-09 15:22:23 +0200 |
commit | 97ff21c74619269a8c7dc9d8ff1485200db58d8c (patch) | |
tree | 22a9301b1c961478c4ed7b3a71b8bcd283aa5e3c /app/src/test/java | |
parent | adc6f7672c380d534fc963d868fe5aebcab54b4a (diff) |
#8758 fix switching providers
Diffstat (limited to 'app/src/test/java')
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/TestUtils.java | 26 | ||||
-rw-r--r-- | app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java | 84 |
2 files changed, 110 insertions, 0 deletions
diff --git a/app/src/test/java/se/leap/bitmaskclient/TestUtils.java b/app/src/test/java/se/leap/bitmaskclient/TestUtils.java new file mode 100644 index 00000000..96b11df6 --- /dev/null +++ b/app/src/test/java/se/leap/bitmaskclient/TestUtils.java @@ -0,0 +1,26 @@ +package se.leap.bitmaskclient; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * Created by cyberta on 08.10.17. + */ + +public class TestUtils { + + public static String getInputAsString(InputStream fileAsInputStream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(fileAsInputStream)); + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine(); + } + + return sb.toString(); + } + +} 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 |