summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap/bitmaskclient/providersetup/ProviderManagerTest.java
blob: 7584fb3f56a71fafcaee1591cfd259382fe1ec16 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package se.leap.bitmaskclient.providersetup;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper;

import android.content.res.AssetManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;

import se.leap.bitmaskclient.base.models.Provider;
import se.leap.bitmaskclient.base.utils.FileHelper;
import se.leap.bitmaskclient.base.utils.InputStreamHelper;
import se.leap.bitmaskclient.testutils.MockHelper;

/**
 * Created by cyberta on 20.02.18.
 */
public class ProviderManagerTest {

    private AssetManager assetManager;
    private File file;
    private ProviderManager providerManager;
    InputStreamHelper inputStreamHelper;
    FileHelper fileHelper;
    MockHelper.MockFileHelper mockedFileHelperInterface;

    @Before
    public void setup() throws Exception {
        assetManager = mock(AssetManager.class);
        file = mock(File.class);

        when(assetManager.open(anyString())).thenAnswer(new Answer<InputStream>() {
            @Override
            public InputStream answer(InvocationOnMock invocation) throws Throwable {
                String filename = "preconfigured/" + invocation.getArguments()[0];
                return getClass().getClassLoader().getResourceAsStream(filename);
            }
        });
        when(assetManager.list(anyString())).thenAnswer(new Answer<String[]>() {
            @Override
            public String[] answer(InvocationOnMock invocation) throws Throwable {
                String path = (String) invocation.getArguments()[0];
                if ("urls".equals(path)) {
                    String[] preconfiguredUrls = new String[3];
                    preconfiguredUrls[0] = "calyx.net.url";
                    preconfiguredUrls[1] = "demo.bitmask.net.url";
                    preconfiguredUrls[2] = "riseup.net.url";
                    return preconfiguredUrls;
                } else
                    throw new IllegalArgumentException("You need to implement the expected path manually!");
            }
        });

        //mock File methods
        //------------------
        when(file.isDirectory()).thenReturn(true);

        ArrayList<String> mockedCustomProviderList = new ArrayList<>();
        mockedCustomProviderList.add("leapcolombia.json");
        String[] mockedCustomProviderArray = new String[mockedCustomProviderList.size()];
        mockedCustomProviderArray = mockedCustomProviderList.toArray(mockedCustomProviderArray);
        when(file.list()).thenReturn(mockedCustomProviderArray);

        when(file.getAbsolutePath()).thenReturn("externalDir");
        when(file.getPath()).thenReturn("externalDir");
        mockedFileHelperInterface = new MockHelper.MockFileHelper(file);
        fileHelper = new FileHelper(mockedFileHelperInterface);
        inputStreamHelper = mockInputStreamHelper();

    }

    @After
    public void tearDown() {
        ProviderManager.reset();
    }

    @Test
    public void testSize_dummyEntry_has5ProvidersWithCurrentTestSetup() {
        providerManager = ProviderManager.getInstance(assetManager, file);
        providerManager.setAddDummyEntry(true);
        assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.size());
    }

    @Test
    public void testSize_has4ProvidersWithCurrentTestSetup() {
        providerManager = ProviderManager.getInstance(assetManager, file);
        assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.size());
    }


    @Test
    public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        providerManager.setAddDummyEntry(true);
        Provider customProvider = new Provider("https://anewprovider.org");
        assertTrue("custom provider added: ", providerManager.add(customProvider));
        assertEquals("3 preconfigured, 2 custom providers, 1 dummy provider", 6, providerManager.providers().size());
    }

    @Test
    public void testAdd_newCustomProviderThatIsNotPartOfDefaultNorCustomList_returnTrue() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://anewprovider.org");
        assertTrue("custom provider added: ", providerManager.add(customProvider));
        assertEquals("3 preconfigured, 2 custom providers", 5, providerManager.providers().size());
    }

    @Test
    public void testAdd_dummyEntry_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        providerManager.setAddDummyEntry(true);
        Provider customProvider = new Provider("https://leapcolombia.org");
        assertFalse("custom provider added: ", providerManager.add(customProvider));
        assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.providers().size());
    }

    @Test
    public void testAdd_newCustomProviderThatIsNotPartOfDefaultButOfCustomList_returnFalse() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://leapcolombia.org");
        assertFalse("custom provider added: ", providerManager.add(customProvider));
        assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size());
    }

    @Test
    public void testAdd_newCustomProviderThatIsPartOfDefaultButNotOfCustomList_returnFalse() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://demo.bitmask.net");
        assertFalse("custom provider added: ", providerManager.add(customProvider));
        assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size());
    }

    @Test
    public void testRemove_ProviderIsPartOfDefaultButNotCustomList_returnsFalse() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://demo.bitmask.net");
        assertFalse("custom provider not removed: ", providerManager.remove(customProvider));
        assertEquals("3 preconfigured, 1 custom provider", 4, providerManager.providers().size());
    }

    @Test
    public void testRemove_ProviderIsNotPartOfDefaultButOfCustomList_returnsTrue() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://leapcolombia.org");
        assertTrue("custom provider not removed: ", providerManager.remove(customProvider));
        assertEquals("3 preconfigured, 0 custom providers", 3, providerManager.providers().size());
    }

    @Test
    public void testRemove_ProviderIsNotPartOfDefaultNorOfCustomList_returnsFalse() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://anotherprovider.org");
        assertFalse("custom provider not removed: ", providerManager.remove(customProvider));
        assertEquals("3 preconfigured, 1 custom providers", 4, providerManager.providers().size());
    }

    @Test
    public void testClear_dummyEntry_ProvidersListHasOnlyDummyProvider() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        providerManager.setAddDummyEntry(true);
        providerManager.clear();
        assertEquals("1 providers", 1, providerManager.providers().size());
        assertEquals("provider is dummy element", "https://example.net", providerManager.get(0).getMainUrlString());
    }

    @Test
    public void testClear_noEntries() throws Exception {
        providerManager = ProviderManager.getInstance(assetManager, file);
        providerManager.clear();
        assertEquals("no providers", 0, providerManager.providers().size());
    }

    @Test
    public void testSaveCustomProvidersToFile_CustomProviderDeleted_deletesFromDir() throws Exception {
        when(file.exists()).thenReturn(true);
        providerManager = ProviderManager.getInstance(assetManager, file);
        //leapcolombia is mocked custom provider from setup
        Provider customProvider = new Provider("https://leapcolombia.org");
        providerManager.remove(customProvider);
        providerManager.saveCustomProvidersToFile();
        verify(file, times(1)).delete();
    }


    @Test
    public void testSaveCustomProvidersToFile_newCustomProviders_persistNew() throws Exception {
        when(file.list()).thenReturn(new String[0]);
        when(file.exists()).thenReturn(false);
        providerManager = ProviderManager.getInstance(assetManager, file);
        Provider customProvider = new Provider("https://anotherprovider.org");
        Provider secondCustomProvider = new Provider("https://yetanotherprovider.org");
        providerManager.add(customProvider);
        providerManager.add(secondCustomProvider);
        providerManager.saveCustomProvidersToFile();

        assertEquals("persist was called twice", 2, mockedFileHelperInterface.getPersistFileCounter());
    }


}