1 /*
2  * Copyright 2020 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 
17 package com.google.android.iwlan;
18 
19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
20 
21 import static org.junit.Assert.assertNotNull;
22 import static org.mockito.Mockito.anyInt;
23 import static org.mockito.Mockito.eq;
24 import static org.mockito.Mockito.timeout;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.annotation.Nullable;
29 import android.content.Context;
30 import android.net.ConnectivityManager;
31 import android.net.Network;
32 import android.net.NetworkCapabilities;
33 import android.net.TelephonyNetworkSpecifier;
34 import android.net.vcn.VcnTransportInfo;
35 import android.telephony.AccessNetworkConstants;
36 import android.telephony.INetworkService;
37 import android.telephony.INetworkServiceCallback;
38 import android.telephony.NetworkRegistrationInfo;
39 import android.telephony.NetworkServiceCallback;
40 import android.telephony.SubscriptionInfo;
41 import android.telephony.SubscriptionManager;
42 import android.telephony.TelephonyManager;
43 import android.telephony.ims.ImsManager;
44 import android.telephony.ims.ImsMmTelManager;
45 
46 import com.google.android.iwlan.IwlanNetworkService.IwlanNetworkServiceProvider;
47 
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.mockito.MockitoSession;
54 import org.mockito.quality.Strictness;
55 
56 import java.util.List;
57 
58 public class IwlanNetworkServiceTest {
59     private static final String TAG = IwlanNetworkServiceTest.class.getSimpleName();
60     private static final int DEFAULT_SLOT_INDEX = 0;
61     private static final int DEFAULT_SUB_INDEX = 0;
62 
63     @Mock private Context mMockContext;
64     @Mock private ConnectivityManager mMockConnectivityManager;
65     @Mock private SubscriptionManager mMockSubscriptionManager;
66     @Mock private SubscriptionInfo mMockSubscriptionInfo;
67     @Mock private ImsManager mMockImsManager;
68     @Mock private ImsMmTelManager mMockImsMmTelManager;
69     @Mock private INetworkServiceCallback mCallback;
70     @Mock private Network mMockNetwork;
71     MockitoSession mStaticMockSession;
72 
73     IwlanNetworkService mIwlanNetworkService;
74     INetworkService mBinder;
75     IwlanNetworkServiceProvider mIwlanNetworkServiceProvider;
76 
77     @Before
setUp()78     public void setUp() throws Exception {
79         MockitoAnnotations.initMocks(this);
80 
81         mStaticMockSession =
82                 mockitoSession()
83                         .mockStatic(SubscriptionManager.class)
84                         .strictness(Strictness.LENIENT)
85                         .startMocking();
86 
87         when(mMockContext.getSystemService(eq(ConnectivityManager.class)))
88                 .thenReturn(mMockConnectivityManager);
89 
90         when(mMockContext.getSystemService(eq(SubscriptionManager.class)))
91                 .thenReturn(mMockSubscriptionManager);
92 
93         when(mMockSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(
94                         eq(DEFAULT_SLOT_INDEX)))
95                 .thenReturn(mMockSubscriptionInfo);
96         when(SubscriptionManager.getDefaultDataSubscriptionId()).thenReturn(DEFAULT_SUB_INDEX);
97         when(SubscriptionManager.getSlotIndex(DEFAULT_SUB_INDEX)).thenReturn(DEFAULT_SLOT_INDEX);
98         when(SubscriptionManager.getSlotIndex(DEFAULT_SUB_INDEX + 1))
99                 .thenReturn(DEFAULT_SLOT_INDEX + 1);
100 
101         when(mMockSubscriptionInfo.getSubscriptionId()).thenReturn(DEFAULT_SUB_INDEX);
102 
103         when(mMockContext.getSystemService(eq(ImsManager.class))).thenReturn(mMockImsManager);
104 
105         when(mMockImsManager.getImsMmTelManager(anyInt())).thenReturn(mMockImsMmTelManager);
106 
107         mIwlanNetworkService = new IwlanNetworkService();
108         mIwlanNetworkService.setAppContext(mMockContext);
109         mIwlanNetworkServiceProvider = null;
110 
111         mBinder = mIwlanNetworkService.mBinder;
112         mBinder.createNetworkServiceProvider(DEFAULT_SLOT_INDEX);
113         mBinder.registerForNetworkRegistrationInfoChanged(DEFAULT_SLOT_INDEX, mCallback);
114     }
115 
116     @After
cleanUp()117     public void cleanUp() throws Exception {
118         mBinder.removeNetworkServiceProvider(DEFAULT_SLOT_INDEX);
119         mStaticMockSession.finishMocking();
120     }
121 
122     @Nullable
initNSP()123     IwlanNetworkServiceProvider initNSP() {
124         // Wait for IwlanNetworkServiceProvider created and timeout is 1 second.
125         long startTime = System.currentTimeMillis();
126         IwlanNetworkServiceProvider nsp = null;
127         while (System.currentTimeMillis() - startTime < 1000) {
128             nsp = mIwlanNetworkService.getNetworkServiceProvider(DEFAULT_SLOT_INDEX);
129             if (nsp != null) {
130                 break;
131             }
132         }
133         return nsp;
134     }
135 
136     @Test
testRequestNetworkRegistrationInfo()137     public void testRequestNetworkRegistrationInfo() throws Exception {
138         mIwlanNetworkServiceProvider = initNSP();
139         assertNotNull(mIwlanNetworkServiceProvider);
140 
141         // Set Wifi on and verify mCallback should receive onNetworkStateChanged.
142         IwlanNetworkService.setNetworkConnected(true, IwlanNetworkService.Transport.WIFI);
143         verify(mCallback, timeout(1000).times(1)).onNetworkStateChanged();
144 
145         // Set Sub active and verify mCallback should receive onNetworkStateChanged.
146         mIwlanNetworkServiceProvider.subscriptionChanged();
147         verify(mCallback, timeout(1000).times(2)).onNetworkStateChanged();
148 
149         // Create expected NetworkRegistrationInfo
150         NetworkRegistrationInfo.Builder expectedStateBuilder =
151                 generateStateBuilder(
152                         NetworkRegistrationInfo.DOMAIN_PS,
153                         true /* isSubActive */,
154                         NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
155 
156         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
157 
158         verify(mCallback, timeout(1000).times(1))
159                 .onRequestNetworkRegistrationInfoComplete(
160                         eq(NetworkServiceCallback.RESULT_SUCCESS),
161                         eq(expectedStateBuilder.build()));
162     }
163 
prepareCellularNetworkCapabilitiesForTest( int subId, boolean isVcn)164     private NetworkCapabilities prepareCellularNetworkCapabilitiesForTest(
165             int subId, boolean isVcn) {
166         NetworkCapabilities.Builder builder =
167                 new NetworkCapabilities.Builder()
168                         .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
169         if (isVcn) {
170             builder.setTransportInfo(new VcnTransportInfo(subId));
171         } else {
172             builder.setNetworkSpecifier(new TelephonyNetworkSpecifier(subId));
173         }
174         return builder.build();
175     }
176 
prepareWifiNetworkCapabilitiesForTest()177     private NetworkCapabilities prepareWifiNetworkCapabilitiesForTest() {
178         return new NetworkCapabilities.Builder()
179                 .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
180                 .build();
181     }
182 
183     @Test
testNetworkRegistrationInfoSearchingForCellularAndCstDisabled()184     public void testNetworkRegistrationInfoSearchingForCellularAndCstDisabled() throws Exception {
185         mIwlanNetworkServiceProvider = initNSP();
186         assertNotNull(mIwlanNetworkServiceProvider);
187 
188         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(false);
189 
190         NetworkCapabilities nc =
191                 prepareCellularNetworkCapabilitiesForTest(DEFAULT_SUB_INDEX, false /* is Vcn */);
192         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
193         mIwlanNetworkServiceProvider.subscriptionChanged();
194 
195         // Create expected NetworkRegistrationInfo
196         NetworkRegistrationInfo.Builder expectedStateBuilder =
197                 generateStateBuilder(
198                         NetworkRegistrationInfo.DOMAIN_PS,
199                         true /* isSubActive */,
200                         NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING);
201 
202         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
203 
204         verify(mCallback, timeout(1000).times(1))
205                 .onRequestNetworkRegistrationInfoComplete(
206                         eq(NetworkServiceCallback.RESULT_SUCCESS),
207                         eq(expectedStateBuilder.build()));
208     }
209 
210     @Test
testNetworkRegistrationInfoSearchingForCellularOnSameSubAndCstEnabled()211     public void testNetworkRegistrationInfoSearchingForCellularOnSameSubAndCstEnabled()
212             throws Exception {
213         mIwlanNetworkServiceProvider = initNSP();
214         assertNotNull(mIwlanNetworkServiceProvider);
215 
216         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(true);
217 
218         NetworkCapabilities nc =
219                 prepareCellularNetworkCapabilitiesForTest(DEFAULT_SUB_INDEX, false /* is Vcn */);
220         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
221         mIwlanNetworkServiceProvider.subscriptionChanged();
222 
223         // Create expected NetworkRegistrationInfo
224         NetworkRegistrationInfo.Builder expectedStateBuilder =
225                 generateStateBuilder(
226                         NetworkRegistrationInfo.DOMAIN_PS,
227                         true /* mIsSubActive */,
228                         NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING);
229 
230         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
231 
232         verify(mCallback, timeout(1000).times(1))
233                 .onRequestNetworkRegistrationInfoComplete(
234                         eq(NetworkServiceCallback.RESULT_SUCCESS),
235                         eq(expectedStateBuilder.build()));
236     }
237 
238     @Test
testNetworkRegistrationInfoHomeForCellularOnDifferentSubAndCstEnabled()239     public void testNetworkRegistrationInfoHomeForCellularOnDifferentSubAndCstEnabled()
240             throws Exception {
241         mIwlanNetworkServiceProvider = initNSP();
242         assertNotNull(mIwlanNetworkServiceProvider);
243 
244         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(true);
245 
246         // Cellular data is on the other sub
247         NetworkCapabilities nc =
248                 prepareCellularNetworkCapabilitiesForTest(
249                         DEFAULT_SUB_INDEX + 1, false /* is Vcn */);
250         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
251         mIwlanNetworkServiceProvider.subscriptionChanged();
252 
253         // Create expected NetworkRegistrationInfo
254         NetworkRegistrationInfo.Builder expectedStateBuilder =
255                 generateStateBuilder(
256                         NetworkRegistrationInfo.DOMAIN_PS,
257                         true /* isSubActive */,
258                         NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
259 
260         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
261 
262         verify(mCallback, timeout(1000).times(1))
263                 .onRequestNetworkRegistrationInfoComplete(
264                         eq(NetworkServiceCallback.RESULT_SUCCESS),
265                         eq(expectedStateBuilder.build()));
266     }
267 
268     @Test
testNetworkRegistrationInfoHomeForCellularVcnOnDifferentSubAndCstEnabled()269     public void testNetworkRegistrationInfoHomeForCellularVcnOnDifferentSubAndCstEnabled()
270             throws Exception {
271         mIwlanNetworkServiceProvider = initNSP();
272         assertNotNull(mIwlanNetworkServiceProvider);
273 
274         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(true);
275 
276         // Cellular data as a VCN network is on the other sub
277         NetworkCapabilities nc =
278                 prepareCellularNetworkCapabilitiesForTest(DEFAULT_SUB_INDEX + 1, true /* is Vcn */);
279         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
280         mIwlanNetworkServiceProvider.subscriptionChanged();
281 
282         // Create expected NetworkRegistrationInfo
283         NetworkRegistrationInfo.Builder expectedStateBuilder =
284                 generateStateBuilder(
285                         NetworkRegistrationInfo.DOMAIN_PS,
286                         true /* isSubActive */,
287                         NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
288 
289         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
290 
291         verify(mCallback, timeout(1000).times(1))
292                 .onRequestNetworkRegistrationInfoComplete(
293                         eq(NetworkServiceCallback.RESULT_SUCCESS),
294                         eq(expectedStateBuilder.build()));
295     }
296 
297     @Test
testNetworkRegistrationInfoHomeForWiFiAndCstEnabled()298     public void testNetworkRegistrationInfoHomeForWiFiAndCstEnabled() throws Exception {
299         mIwlanNetworkServiceProvider = initNSP();
300         assertNotNull(mIwlanNetworkServiceProvider);
301 
302         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(true);
303 
304         NetworkCapabilities nc = prepareWifiNetworkCapabilitiesForTest();
305         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
306         mIwlanNetworkServiceProvider.subscriptionChanged();
307 
308         // Create expected NetworkRegistrationInfo
309         NetworkRegistrationInfo.Builder expectedStateBuilder =
310                 generateStateBuilder(
311                         NetworkRegistrationInfo.DOMAIN_PS,
312                         true /* isSubActive */,
313                         NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
314 
315         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
316 
317         verify(mCallback, timeout(1000).times(1))
318                 .onRequestNetworkRegistrationInfoComplete(
319                         eq(NetworkServiceCallback.RESULT_SUCCESS),
320                         eq(expectedStateBuilder.build()));
321     }
322 
323     @Test
testNetworkRegistrationInfoHomeForWiFiAndCstDisabled()324     public void testNetworkRegistrationInfoHomeForWiFiAndCstDisabled() throws Exception {
325         mIwlanNetworkServiceProvider = initNSP();
326         assertNotNull(mIwlanNetworkServiceProvider);
327 
328         when(mMockImsMmTelManager.isCrossSimCallingEnabled()).thenReturn(false);
329 
330         NetworkCapabilities nc = prepareWifiNetworkCapabilitiesForTest();
331         mIwlanNetworkService.getNetworkMonitorCallback().onCapabilitiesChanged(mMockNetwork, nc);
332 
333         mIwlanNetworkServiceProvider.subscriptionChanged();
334 
335         // Create expected NetworkRegistrationInfo
336         NetworkRegistrationInfo.Builder expectedStateBuilder =
337                 generateStateBuilder(
338                         NetworkRegistrationInfo.DOMAIN_PS,
339                         true /* isSubActive */,
340                         NetworkRegistrationInfo.REGISTRATION_STATE_HOME);
341 
342         mBinder.requestNetworkRegistrationInfo(0, NetworkRegistrationInfo.DOMAIN_PS, mCallback);
343 
344         verify(mCallback, timeout(1000).times(1))
345                 .onRequestNetworkRegistrationInfoComplete(
346                         eq(NetworkServiceCallback.RESULT_SUCCESS),
347                         eq(expectedStateBuilder.build()));
348     }
349 
generateStateBuilder( int domain, boolean isSubActive, int registrationState)350     private NetworkRegistrationInfo.Builder generateStateBuilder(
351             int domain, boolean isSubActive, int registrationState) {
352         NetworkRegistrationInfo.Builder expectedStateBuilder =
353                 new NetworkRegistrationInfo.Builder();
354         expectedStateBuilder
355                 .setAccessNetworkTechnology(
356                         (registrationState
357                                         == NetworkRegistrationInfo
358                                                 .REGISTRATION_STATE_NOT_REGISTERED_SEARCHING)
359                                 ? TelephonyManager.NETWORK_TYPE_UNKNOWN
360                                 : TelephonyManager.NETWORK_TYPE_IWLAN)
361                 .setAvailableServices(List.of(NetworkRegistrationInfo.SERVICE_TYPE_DATA))
362                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
363                 .setEmergencyOnly(!isSubActive)
364                 .setDomain(domain)
365                 .setRegistrationState(registrationState);
366 
367         return expectedStateBuilder;
368     }
369 }
370