summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap/bitmaskclient/eip/GatewaysManagerTest.java
blob: 833346fac660272bc7b9c68d522edd79f5d3f057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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.VPN_CERTIFICATE), anyString())).thenReturn(secrets.getString(Constants.VPN_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));
    }

}