summaryrefslogtreecommitdiff
path: root/app/src/test/java/se/leap/bitmaskclient/tethering/TetheringStateManagerTest.java
blob: a5954a9782ca039bdeb4fe662108d7d996ba7821 (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
/**
 * Copyright (c) 2020 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.tethering;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;

public class TetheringStateManagerTest {

    private TetheringObservable observable;

    @Before
    public void setup() throws Exception {
        observable = TetheringObservable.getInstance();
    }

    @Test
    public void testGetWifiAddressRangee_keepsLastSeenAddressAndInterface() throws Exception {
        //WifiTethering was switched on
        TetheringObservable.setWifiTethering(true, "192.168.40.0/24", "wlan0");

        assertEquals("192.168.40.0/24", observable.getTetheringState().wifiAddress);
        assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenWifiAddress);
        assertEquals("wlan0", observable.getTetheringState().wifiInterface);
        assertEquals("wlan0", observable.getTetheringState().lastSeenWifiInterface);
        //Wifi tethering was switched off
        TetheringObservable.setWifiTethering(true, "", "");
        assertEquals("", observable.getTetheringState().wifiAddress);
        assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenWifiAddress);
        assertEquals("", observable.getTetheringState().wifiInterface);
        assertEquals("wlan0", observable.getTetheringState().lastSeenWifiInterface);
    }

    @Test
    public void testGetUsbAddressRange_keepsLastSeenAddressAndInterface() throws Exception {
        WifiManagerWrapper mockWrapper = mock(WifiManagerWrapper.class);
        when(mockWrapper.isWifiAPEnabled()).thenReturn(true);
        PowerMockito.whenNew(WifiManagerWrapper.class).withAnyArguments().thenReturn(mockWrapper);

        //UsbTethering was switched on
        TetheringObservable.setUsbTethering(true, "192.168.40.0/24", "rndis0");
        assertEquals("192.168.40.0/24", observable.getTetheringState().usbAddress);
        assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenUsbAddress);
        assertEquals("rndis0", observable.getTetheringState().usbInterface);
        assertEquals("rndis0", observable.getTetheringState().lastSeenUsbInterface);
        //UsbTethering tethering was switched off
        TetheringObservable.setUsbTethering(true, "", "");
        assertEquals("", observable.getTetheringState().usbAddress);
        assertEquals("192.168.40.0/24", observable.getTetheringState().lastSeenUsbAddress);
        assertEquals("", observable.getTetheringState().usbInterface);
        assertEquals("rndis0", observable.getTetheringState().lastSeenUsbInterface);
    }


}