summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/transport/WiFiSocketFactoryTest.java
blob: 8cbaa3c72f5947bb66f8010a8c1d726ad09a0949 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package com.smartdevicelink.test.transport;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.util.Log;

import com.smartdevicelink.transport.utl.WiFiSocketFactory;

import junit.framework.TestCase;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import javax.net.SocketFactory;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * This is a unit test class for the WiFiSocketFactory class:
 * {@link com.smartdevicelink.transport.utl.WiFiSocketFactory}
 * <p>
 * Requires LOLLIPOP or later since the tests use android.net.NetworkCapabilities class
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class WiFiSocketFactoryTest extends TestCase {

    private static final String TAG = WiFiSocketFactoryTest.class.getSimpleName();

    private Context mMockContext;
    private PackageManager mMockPackageManager;
    private ConnectivityManager mMockConnMan;
    private SocketFactory mMockSocketFactory; // this is the SocketFactory that creates mWiFiBoundSocket
    private Socket mWiFiBoundSocket;

    private enum FactoryRet {
        RETURNS_NULL,
        RETURNS_CORRECT_FACTORY,
        RETURNS_ANOTHER_FACTORY,
    }

    private class MockNetworkConfig {
        // true to make a null Network
        boolean isNull;
        // specify the transport type of the Network
        int transportType;
        // spcify the type of SocketFactory returned from this Network
        FactoryRet factoryReturnValue;

        MockNetworkConfig(boolean isNull, int transportType, FactoryRet factoryReturnValue) {
            this.isNull = isNull;
            this.transportType = transportType;
            this.factoryReturnValue = factoryReturnValue;
        }
    }

    private void setupMockNetworks(MockNetworkConfig[] configs) {
        if (configs == null) {
            when(mMockConnMan.getAllNetworks()).thenReturn(null);
            return;
        }

        List<Network> networkList = new ArrayList<Network>(configs.length);

        for (MockNetworkConfig config : configs) {
            if (config.isNull) {
                networkList.add(null);
                continue;
            }

            Network network = mock(Network.class);

            NetworkCapabilities networkCapabilities = createNetworkCapabilitiesWithTransport(config.transportType);
            when(mMockConnMan.getNetworkCapabilities(network)).thenReturn(networkCapabilities);

            SocketFactory factory = null;
            switch (config.factoryReturnValue) {
                case RETURNS_NULL:
                    break;
                case RETURNS_CORRECT_FACTORY:
                    factory = mMockSocketFactory;
                    break;
                case RETURNS_ANOTHER_FACTORY:
                    // create another mock SocketFactory instance
                    factory = mock(SocketFactory.class);
                    break;
            }
            when(network.getSocketFactory()).thenReturn(factory);

            networkList.add(network);
        }

        when(mMockConnMan.getAllNetworks()).thenReturn(networkList.toArray(new Network[networkList.size()]));
    }

    private static NetworkCapabilities createNetworkCapabilitiesWithTransport(int transport) {
        // Creates a dummy NetworkCapabilities instance.
        // Since NetworkCapabilities class is 'final', we cannot create its mock. To create a dummy
        // instance, here we use reflection to call its constructor and a method that are marked
        // with "@hide".
        // It is possible that these methods will not be available in a future version of Android.
        // In that case we need to update our code accordingly.
        Class<NetworkCapabilities> c = NetworkCapabilities.class;
        try {
            Method addTransportTypeMethod = c.getMethod("addTransportType", int.class);
            addTransportTypeMethod.setAccessible(true);

            NetworkCapabilities instance = c.getDeclaredConstructor().newInstance();
            addTransportTypeMethod.invoke(instance, transport);
            Log.e(TAG, "Yes successful");
            return instance;
        } catch (Exception e) {
            Log.e(TAG, "Failed to create NetworkCapabilities instance using reflection: ", e);
            return null;
        }
    }

    // from https://stackoverflow.com/questions/40300469/mock-build-version-with-mockito
    // and https://stackoverflow.com/questions/13755117/android-changing-private-static-final-field-using-java-reflection
    private static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);
//        Field modifiersField = Field.class.getDeclaredField("modifiers");
        // This call might fail on some devices (for example, Nexus 6 with Android 5.0.1).
        // If that's the issue, we might want to introduce PowerMock.
        Field modifiersField = Field.class.getDeclaredField("accessFlags");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, newValue);
    }


    @Override
    public void setUp() throws Exception {
        super.setUp();

        mMockContext = mock(Context.class);
        mMockPackageManager = mock(PackageManager.class);
        mMockConnMan = mock(ConnectivityManager.class);

        when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
        when(mMockContext.getPackageName()).thenReturn("dummyPackageName");
        when(mMockContext.getSystemService(eq(Context.CONNECTIVITY_SERVICE))).thenReturn(mMockConnMan);

        when(mMockPackageManager.checkPermission(eq(Manifest.permission.ACCESS_NETWORK_STATE), anyString())).thenReturn(PackageManager.PERMISSION_GRANTED);

        mMockSocketFactory = mock(SocketFactory.class);
        mWiFiBoundSocket = new Socket();
        when(mMockSocketFactory.createSocket()).thenReturn(mWiFiBoundSocket);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    // test the happy path
    public void testWithWiFiNetwork() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_CELLULAR, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertEquals("Returned Socket should be created through SocketFactory", mWiFiBoundSocket, ret);
    }

    // test the case where SDK_INT is less than 21
    /* This is disabled since Travis CI uses an AVD with 5.1.1 and setFinalStatic() doesn't work on it.
    public void testPriorToLollipop() throws Exception {
        setupMockNetworks(new MockNetworkConfig[] {
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_CELLULAR, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        // simulate SDK_INT to less than LOLLIPOP
        int previousSDKInt = Build.VERSION.SDK_INT;
        setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.KITKAT_WATCH);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        // make sure we revert our change
        setFinalStatic(Build.VERSION.class.getField("SDK_INT"), previousSDKInt);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since it is not available prior to LOLLIPOP",
                mWiFiBoundSocket, ret);
    }
    */

    // test the case where we do not have ACCESS_NETWORK_STATE permission
    public void testWithoutPermission() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        // simulate the case where required permission isn't available
        when(mMockPackageManager.checkPermission(eq(Manifest.permission.ACCESS_NETWORK_STATE), anyString())).thenReturn(PackageManager.PERMISSION_DENIED);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since we don't have required permission",
                mWiFiBoundSocket, ret);
    }

    // test the case where context.getPackageManager() returns null
    public void testPackageManagerNull() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        // simulate the case where ConnectivityManager isn't available
        when(mMockContext.getPackageManager()).thenReturn(null);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since PackageManager isn't available",
                mWiFiBoundSocket, ret);
    }

    // test the case where getSystemService() returns null
    public void testConnectivityManagerNull() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        // simulate the case where ConnectivityManager isn't available
        when(mMockContext.getSystemService(eq(Context.CONNECTIVITY_SERVICE))).thenReturn(null);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since ConnectivityManager isn't working",
                mWiFiBoundSocket, ret);
    }

    // test the case where ConnectivityManager returns null for the network list
    public void testNetworkListNull() {
        setupMockNetworks(null);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since Network list isn't available",
                mWiFiBoundSocket, ret);
    }

    // test the case where the network list contains a null for Network instance
    public void testNetworkListHasNull() {
        setupMockNetworks(new MockNetworkConfig[]{
                // multiple Network instances in the list, the first one being NULL
                new MockNetworkConfig(true, 0, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertEquals("Returned Socket should be created through SocketFactory", mWiFiBoundSocket, ret);
    }

    // test the case where the phone isn't connected to Wi-Fi network
    public void testNoWiFiNetwork() {
        setupMockNetworks(new MockNetworkConfig[]{
                // none of the instances has TRANSPORT_WIFI in their capabilities
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_CELLULAR, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_BLUETOOTH, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_VPN, FactoryRet.RETURNS_ANOTHER_FACTORY),
        });

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since Wi-Fi network isn't available",
                mWiFiBoundSocket, ret);
    }

    // test the case where we get null for SocketFactory
    public void testSocketFactoryNull() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_CELLULAR, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_NULL),
        });

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since SocketFactory isn't available",
                mWiFiBoundSocket, ret);
    }

    // test the case where we get a null for SocketFactory, then a valid one for another
    public void testSocketFactoryNull2() {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_CELLULAR, FactoryRet.RETURNS_ANOTHER_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_NULL),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertEquals("Returned Socket should be created through SocketFactory", mWiFiBoundSocket, ret);
    }

    // test the case where we get an exception with SocketFactory.createSocket()
    public void testFactoryReturnsException() throws IOException {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        when(mMockSocketFactory.createSocket()).thenThrow(new IOException("Dummy IOException for testing!"));

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertNotSame("Returned Socket shouldn't be created through SocketFactory since it throws an IOException",
                mWiFiBoundSocket, ret);
    }

    // Test the case we get multiple Network instances with Wi-Fi transport, and the SocketFactory of
    // the first one throws Exception and the other one succeeds.
    // This is to simulate Samsung Galaxy S9.
    public void testFactoryReturnsException2() throws IOException {
        setupMockNetworks(new MockNetworkConfig[]{
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
                new MockNetworkConfig(false, NetworkCapabilities.TRANSPORT_WIFI, FactoryRet.RETURNS_CORRECT_FACTORY),
        });

        when(mMockSocketFactory.createSocket()).thenThrow(new IOException("Dummy IOException for testing!"))
                .thenReturn(mWiFiBoundSocket);

        Socket ret = WiFiSocketFactory.createSocket(mMockContext);

        assertNotNull("createSocket() should always return a Socket instance", ret);
        assertEquals("Returned Socket should be created through SocketFactory", mWiFiBoundSocket, ret);
    }
}