1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import static org.junit.Assert.*;
20import static org.mockito.Mockito.*;
21
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.net.wifi.EAPConstants;
27import android.net.wifi.WifiEnterpriseConfig;
28import android.os.PersistableBundle;
29import android.telephony.CarrierConfigManager;
30import android.telephony.SubscriptionInfo;
31import android.telephony.SubscriptionManager;
32import android.test.suitebuilder.annotation.SmallTest;
33import android.util.Base64;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.mockito.ArgumentCaptor;
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
40
41import java.util.Arrays;
42
43/**
44 * Unit tests for {@link com.android.server.wifi.CarrierNetworkConfig}.
45 */
46@SmallTest
47public class CarrierNetworkConfigTest {
48    private static final String TEST_SSID = "Test SSID";
49    private static final int TEST_STANDARD_EAP_TYPE = EAPConstants.EAP_SIM;
50    private static final int TEST_INTERNAL_EAP_TYPE = WifiEnterpriseConfig.Eap.SIM;
51    private static final int TEST_SUBSCRIPTION_ID = 1;
52    private static final String TEST_CARRIER_NAME = "Test Carrier";
53    private static final SubscriptionInfo TEST_SUBSCRIPTION_INFO =
54            new SubscriptionInfo(TEST_SUBSCRIPTION_ID, null, 0, TEST_CARRIER_NAME, null, 0, 0,
55                    null, 0, null, 0, 0, null);
56
57    @Mock Context mContext;
58    @Mock CarrierConfigManager mCarrierConfigManager;
59    @Mock SubscriptionManager mSubscriptionManager;
60    BroadcastReceiver mBroadcastReceiver;
61    CarrierNetworkConfig mCarrierNetworkConfig;
62
63    /**
64     * Generate and return a carrier config for testing
65     *
66     * @param ssid The SSID of the carrier network
67     * @param eapType The EAP type of the carrier network
68     * @return {@link PersistableBundle} containing carrier config
69     */
70    private PersistableBundle generateTestConfig(String ssid, int eapType) {
71        PersistableBundle bundle = new PersistableBundle();
72        String networkConfig =
73                new String(Base64.encode(ssid.getBytes(), Base64.DEFAULT)) + "," + eapType;
74        bundle.putStringArray(CarrierConfigManager.KEY_CARRIER_WIFI_STRING_ARRAY,
75                new String[] {networkConfig});
76        return bundle;
77    }
78
79    /**
80     * Method to initialize mocks for tests.
81     */
82    @Before
83    public void setUp() throws Exception {
84        MockitoAnnotations.initMocks(this);
85        when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE))
86                .thenReturn(mCarrierConfigManager);
87        when(mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
88                .thenReturn(mSubscriptionManager);
89        when(mCarrierConfigManager.getConfigForSubId(TEST_SUBSCRIPTION_ID))
90                .thenReturn(generateTestConfig(TEST_SSID, TEST_STANDARD_EAP_TYPE));
91        when(mSubscriptionManager.getActiveSubscriptionInfoList())
92                .thenReturn(Arrays.asList(new SubscriptionInfo[] {TEST_SUBSCRIPTION_INFO}));
93        mCarrierNetworkConfig = new CarrierNetworkConfig(mContext);
94        ArgumentCaptor<BroadcastReceiver> receiver =
95                ArgumentCaptor.forClass(BroadcastReceiver.class);
96        verify(mContext).registerReceiver(receiver.capture(), any(IntentFilter.class));
97        mBroadcastReceiver = receiver.getValue();
98        reset(mCarrierConfigManager);
99    }
100
101    /**
102     * Verify that {@link CarrierNetworkConfig#isCarrierNetwork} will return true and
103     * {@link CarrierNetworkConfig#getNetworkEapType} will return the corresponding EAP type
104     * when the given SSID is associated with a carrier network.
105     *
106     * @throws Exception
107     */
108    @Test
109    public void getExistingCarrierNetworkInfo() throws Exception {
110        assertTrue(mCarrierNetworkConfig.isCarrierNetwork(TEST_SSID));
111        assertEquals(TEST_INTERNAL_EAP_TYPE, mCarrierNetworkConfig.getNetworkEapType(TEST_SSID));
112        assertEquals(TEST_CARRIER_NAME, mCarrierNetworkConfig.getCarrierName(TEST_SSID));
113    }
114
115    /**
116     * Verify that {@link CarrierNetworkConfig#isCarrierNetwork} will return false and
117     * {@link CarrierNetworkConfig#getNetworkEapType} will return -1 when the given SSID is not
118     * associated with any carrier network.
119     *
120     * @throws Exception
121     */
122    @Test
123    public void getNonCarrierNetworkInfo() throws Exception {
124        String dummySsid = "Dummy SSID";
125        assertFalse(mCarrierNetworkConfig.isCarrierNetwork(dummySsid));
126        assertEquals(-1, mCarrierNetworkConfig.getNetworkEapType(dummySsid));
127    }
128
129    /**
130     * Verify that the carrier network config is updated when
131     * {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} intent is received.
132     *
133     * @throws Exception
134     */
135    @Test
136    public void receivedCarrierConfigChangedIntent() throws Exception {
137        String updatedSsid = "Updated SSID";
138        int updatedStandardEapType = EAPConstants.EAP_AKA;
139        int updatedInternalEapType = WifiEnterpriseConfig.Eap.AKA;
140        String updatedCarrierName = "Updated Carrier";
141        SubscriptionInfo updatedSubscriptionInfo = new SubscriptionInfo(TEST_SUBSCRIPTION_ID,
142                null, 0, updatedCarrierName, null, 0, 0, null, 0, null, 0, 0, null);
143        when(mSubscriptionManager.getActiveSubscriptionInfoList())
144                .thenReturn(Arrays.asList(new SubscriptionInfo[] {updatedSubscriptionInfo}));
145        when(mCarrierConfigManager.getConfigForSubId(TEST_SUBSCRIPTION_ID))
146                .thenReturn(generateTestConfig(updatedSsid, updatedStandardEapType));
147        mBroadcastReceiver.onReceive(mContext,
148                new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
149
150        // Verify that original SSID no longer associated with a carrier network.
151        assertFalse(mCarrierNetworkConfig.isCarrierNetwork(TEST_SSID));
152        assertEquals(-1, mCarrierNetworkConfig.getNetworkEapType(TEST_SSID));
153        assertEquals(null, mCarrierNetworkConfig.getCarrierName(TEST_SSID));
154
155        // Verify that updated SSID is associated with a carrier network.
156        assertTrue(mCarrierNetworkConfig.isCarrierNetwork(updatedSsid));
157        assertEquals(updatedInternalEapType, mCarrierNetworkConfig.getNetworkEapType(updatedSsid));
158        assertEquals(updatedCarrierName, mCarrierNetworkConfig.getCarrierName(updatedSsid));
159    }
160
161    /**
162     * Verify that the carrier network config is not updated when non
163     * {@link CarrierConfigManager#ACTION_CARRIER_CONFIG_CHANGED} intent is received.
164     *
165     * @throws Exception
166     */
167    @Test
168    public void receivedNonCarrierConfigChangedIntent() throws Exception {
169        mBroadcastReceiver.onReceive(mContext, new Intent("dummyIntent"));
170        verify(mCarrierConfigManager, never()).getConfig();
171    }
172}
173