summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/CarrierNetworkConfigTest.java
blob: d89358df039d4e0e56a931bf8dcd46a369216b95 (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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.net.wifi.EAPConstants;
import android.net.wifi.WifiEnterpriseConfig;
import android.os.PersistableBundle;
import android.os.test.TestLooper;
import android.telephony.CarrierConfigManager;
import android.telephony.ImsiEncryptionInfo;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Base64;

import androidx.test.filters.SmallTest;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.Collections;

/**
 * Unit tests for {@link com.android.server.wifi.CarrierNetworkConfig}.
 */
@SmallTest
public class CarrierNetworkConfigTest {
    private static final String TEST_SSID = "Test SSID";
    private static final int TEST_STANDARD_EAP_TYPE = EAPConstants.EAP_SIM;
    private static final int TEST_INTERNAL_EAP_TYPE = WifiEnterpriseConfig.Eap.SIM;
    private static final int TEST_SUBSCRIPTION_ID = 1;
    private static final String TEST_CARRIER_NAME = "Test Carrier";
    private static final SubscriptionInfo TEST_SUBSCRIPTION_INFO =
            new SubscriptionInfo(TEST_SUBSCRIPTION_ID, null, 0, TEST_CARRIER_NAME, null, 0, 0,
                    null, 0, null, "0", "0", null, false, null, null);

    @Mock Context mContext;
    @Mock CarrierConfigManager mCarrierConfigManager;
    @Mock SubscriptionManager mSubscriptionManager;
    @Mock TelephonyManager mTelephonyManager;
    @Mock TelephonyManager mDataTelephonyManager;
    @Mock PublicKey mPublicKey;
    @Mock FrameworkFacade mFrameworkFacade;
    BroadcastReceiver mBroadcastReceiver;
    CarrierNetworkConfig mCarrierNetworkConfig;
    TestLooper mLooper;
    ContentObserver mContentObserver;

    private ImsiEncryptionInfo mImsiEncryptionInfo = new ImsiEncryptionInfo(null, null, 0, null,
            mPublicKey, null);

    /**
     * Generate and return a carrier config for testing
     *
     * @param ssid The SSID of the carrier network
     * @param eapType The EAP type of the carrier network
     * @return {@link PersistableBundle} containing carrier config
     */
    private PersistableBundle generateTestConfig(String ssid, int eapType) {
        PersistableBundle bundle = new PersistableBundle();
        String networkConfig =
                new String(Base64.encode(ssid.getBytes(), Base64.DEFAULT)) + "," + eapType;
        bundle.putStringArray(CarrierConfigManager.KEY_CARRIER_WIFI_STRING_ARRAY,
                new String[]{networkConfig});
        return bundle;
    }

    /**
     * Method to initialize mocks for tests.
     */
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
                .thenReturn(mCarrierConfigManager);
        when(mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
                .thenReturn(mSubscriptionManager);
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mDataTelephonyManager);
        when(mCarrierConfigManager.getConfigForSubId(TEST_SUBSCRIPTION_ID))
                .thenReturn(generateTestConfig(TEST_SSID, TEST_STANDARD_EAP_TYPE));
        when(mSubscriptionManager.getActiveSubscriptionInfoList())
                .thenReturn(Arrays.asList(new SubscriptionInfo[] {TEST_SUBSCRIPTION_INFO}));
        when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(TelephonyManager.KEY_TYPE_WLAN))
                .thenReturn(mImsiEncryptionInfo);
        mLooper = new TestLooper();
        mCarrierNetworkConfig = new CarrierNetworkConfig(mContext, mLooper.getLooper(),
                mFrameworkFacade);
        ArgumentCaptor<BroadcastReceiver> receiver =
                ArgumentCaptor.forClass(BroadcastReceiver.class);
        verify(mContext).registerReceiver(receiver.capture(), any(IntentFilter.class));
        mBroadcastReceiver = receiver.getValue();
        ArgumentCaptor<ContentObserver> observerCaptor =
                ArgumentCaptor.forClass(ContentObserver.class);
        verify(mFrameworkFacade).registerContentObserver(eq(mContext), any(Uri.class), eq(false),
                observerCaptor.capture());
        mContentObserver = observerCaptor.getValue();
        reset(mCarrierConfigManager);
    }

    /**
     * Verify that {@link CarrierNetworkConfig#isCarrierNetwork} will return true and
     * {@link CarrierNetworkConfig#getNetworkEapType} will return the corresponding EAP type
     * when the given SSID is associated with a carrier network.
     *
     * @throws Exception
     */
    @Test
    public void getExistingCarrierNetworkInfo() throws Exception {
        assertTrue(mCarrierNetworkConfig.isCarrierNetwork(TEST_SSID));
        assertEquals(TEST_INTERNAL_EAP_TYPE, mCarrierNetworkConfig.getNetworkEapType(TEST_SSID));
        assertEquals(TEST_CARRIER_NAME, mCarrierNetworkConfig.getCarrierName(TEST_SSID));
    }

    /**
     * Tests that SubscriptionInfo.getDisplayName() returning null does not throw a
     * NullPointerException in CarrierNetworkConfig.
     */
    @Test
    public void getExistingCarrierNetworkInfo_nullDisplayName_shouldNotThrowNpe() {
        when(mCarrierConfigManager.getConfigForSubId(TEST_SUBSCRIPTION_ID))
                .thenReturn(generateTestConfig(TEST_SSID, TEST_STANDARD_EAP_TYPE));
        SubscriptionInfo testSubscriptionInfoNullDisplayName = new SubscriptionInfo(
                TEST_SUBSCRIPTION_ID, null, 0, null, null, 0, 0,
                null, 0, null, "0", "0", null, false, null, null);
        when(mSubscriptionManager.getActiveSubscriptionInfoList())
                .thenReturn(Collections.singletonList(testSubscriptionInfoNullDisplayName));
        mCarrierNetworkConfig = new CarrierNetworkConfig(mContext, mLooper.getLooper(),
                mFrameworkFacade);
        reset(mCarrierConfigManager);

        assertEquals("", mCarrierNetworkConfig.getCarrierName(TEST_SSID));
    }

    /**
     * Verify that {@link CarrierNetworkConfig#isCarrierEncryptionInfoAvailable} will return true
     * when the carrier IMSI encryption info is available.
     *
     * @throws Exception
     */
    @Test
    public void verifyIsCarrierEncryptionInfoAvailableReturnsTrueWhenEncryptionInfoIsAvailable()
            throws Exception {
        assertTrue(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable());
    }

    /**
     * Verify that {@link CarrierNetworkConfig#isCarrierEncryptionInfoAvailable} will return false
     * when the carrier IMSI encryption info is not available.
     *
     * @throws Exception
     */
    @Test
    public void verifyIsCarrierEncryptionInfoAvailableReturnsFalseWhenEncryptionInfoNotAvailable()
            throws Exception {
        when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(TelephonyManager.KEY_TYPE_WLAN))
                .thenReturn(null);
        mBroadcastReceiver.onReceive(mContext,
                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));

        assertFalse(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable());
    }

    /**
     * Verify that {@link CarrierNetworkConfig#isCarrierNetwork} will return false and
     * {@link CarrierNetworkConfig#getNetworkEapType} will return -1 when the given SSID is not
     * associated with any carrier network.
     *
     * @throws Exception
     */
    @Test
    public void getNonCarrierNetworkInfo() throws Exception {
        String dummySsid = "Dummy SSID";
        assertFalse(mCarrierNetworkConfig.isCarrierNetwork(dummySsid));
        assertEquals(-1, mCarrierNetworkConfig.getNetworkEapType(dummySsid));
    }

    /**
     * Verify that the carrier network config is updated when
     * {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} intent is received.
     *
     * @throws Exception
     */
    @Test
    public void receivedCarrierConfigChangedIntent() throws Exception {
        String updatedSsid = "Updated SSID";
        int updatedStandardEapType = EAPConstants.EAP_AKA;
        int updatedInternalEapType = WifiEnterpriseConfig.Eap.AKA;
        String updatedCarrierName = "Updated Carrier";
        SubscriptionInfo updatedSubscriptionInfo = new SubscriptionInfo(TEST_SUBSCRIPTION_ID,
                null, 0, updatedCarrierName, null, 0, 0, null, 0, null, "0", "0", null,
                false, null, null);
        when(mSubscriptionManager.getActiveSubscriptionInfoList())
                .thenReturn(Arrays.asList(new SubscriptionInfo[] {updatedSubscriptionInfo}));
        when(mCarrierConfigManager.getConfigForSubId(TEST_SUBSCRIPTION_ID))
                .thenReturn(generateTestConfig(updatedSsid, updatedStandardEapType));
        mBroadcastReceiver.onReceive(mContext,
                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));

        // Verify that original SSID no longer associated with a carrier network.
        assertFalse(mCarrierNetworkConfig.isCarrierNetwork(TEST_SSID));
        assertEquals(-1, mCarrierNetworkConfig.getNetworkEapType(TEST_SSID));
        assertEquals(null, mCarrierNetworkConfig.getCarrierName(TEST_SSID));

        // Verify that updated SSID is associated with a carrier network.
        assertTrue(mCarrierNetworkConfig.isCarrierNetwork(updatedSsid));
        assertEquals(updatedInternalEapType, mCarrierNetworkConfig.getNetworkEapType(updatedSsid));
        assertEquals(updatedCarrierName, mCarrierNetworkConfig.getCarrierName(updatedSsid));
    }

    /**
     * Verify that the carrier network config is not updated when non
     * {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} intent is received.
     *
     * @throws Exception
     */
    @Test
    public void receivedNonCarrierConfigChangedIntent() throws Exception {
        mBroadcastReceiver.onReceive(mContext, new Intent("dummyIntent"));
        verify(mCarrierConfigManager, never()).getConfig();
    }

    /**
     * Verify that updateNetworkConfig is called when carrier networks certificates are downloaded.
     */
    @Test
    public void onFeatureDisable_setWifiNetworksAvailableNotificationSettingDisabled() {
        when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(TelephonyManager.KEY_TYPE_WLAN))
                .thenReturn(null);
        mBroadcastReceiver.onReceive(mContext,
                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
        // make sure the initial value is false
        assertFalse(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable());

        when(mDataTelephonyManager.getCarrierInfoForImsiEncryption(TelephonyManager.KEY_TYPE_WLAN))
                .thenReturn(mImsiEncryptionInfo);
        mContentObserver.onChange(false);
        assertTrue(mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable());
    }
}