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

import android.content.res.AssetManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

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

import se.leap.bitmaskclient.utils.ConfigHelper;
import se.leap.bitmaskclient.utils.FileHelper;
import se.leap.bitmaskclient.utils.InputStreamHelper;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static se.leap.bitmaskclient.testutils.MockHelper.mockFileHelper;
import static se.leap.bitmaskclient.testutils.MockHelper.mockInputStreamHelper;

/**
 * Created by cyberta on 20.02.18.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ConfigHelper.class, FileHelper.class, InputStreamHelper.class})
public class ProviderManagerTest {

    @Mock
    private AssetManager assetManager;
    @Mock
    private File file;
    private ProviderManager providerManager;

    @Before
    public void setup() throws Exception {
        //mock assetManager methods
        //--------------------------
        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");
        mockFileHelper(file);

        // mock inputStream
        //-----------------------------------
        mockInputStreamHelper();

    }

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

    @Test
    public void testSize_has5ProvidersWithCurrentTestSetup() {
        providerManager = ProviderManager.getInstance(assetManager, file);
        assertEquals("3 preconfigured, 1 custom provider, 1 dummy provider", 5, providerManager.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, 1 dummy provider", 6, 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, 1 dummy provider", 5, 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, 1 dummy provider", 5, 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, 1 dummy provider", 5, 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, 1 dummy provider", 4, 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, 1 dummy provider", 5, providerManager.providers().size());
    }

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

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

        verifyStatic(FileHelper.class, times(2));
        FileHelper.persistFile(any(File.class), anyString());
    }


}