summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap/bitmaskclient/PreferenceHelperTest.java
blob: d49fa08c91bb2363666e1219441a24351f4f4268 (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
package se.leap.bitmaskclient;

import android.content.SharedPreferences;

import org.junit.Before;
import org.junit.Test;

import se.leap.bitmaskclient.testutils.MockSharedPreferences;
import se.leap.bitmaskclient.utils.ConfigHelper;
import se.leap.bitmaskclient.utils.PreferenceHelper;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED;
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;
import static se.leap.bitmaskclient.utils.PreferenceHelper.providerInSharedPreferences;

/**
 * Created by cyberta on 17.01.18.
 */
public class PreferenceHelperTest {

    private SharedPreferences mockPreferences;


    @Before
    public void setup() {
        mockPreferences = new MockSharedPreferences();
    }

    @Test
    public void providerInSharedPreferences_notInPreferences_returnsFalse() throws Exception {
        assertFalse(providerInSharedPreferences(mockPreferences));
    }

    @Test
    public void providerInSharedPreferences_inPreferences_returnsTrue() throws Exception {
        mockPreferences.edit().putBoolean(PROVIDER_CONFIGURED, true).apply();
        assertTrue(providerInSharedPreferences(mockPreferences));
    }

    @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());
    }


}