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