Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.android.server; |
| 18 | |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 19 | import static android.net.NetworkRecommendationProvider.EXTRA_RECOMMENDATION_RESULT; |
| 20 | import static android.net.NetworkRecommendationProvider.EXTRA_SEQUENCE; |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 21 | import static android.net.NetworkScoreManager.CACHE_FILTER_NONE; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 22 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 23 | import static junit.framework.Assert.assertEquals; |
| 24 | import static junit.framework.Assert.assertFalse; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 25 | import static junit.framework.Assert.assertNotNull; |
Jeremy Joslin | d7670f6 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 26 | import static junit.framework.Assert.assertNull; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 27 | import static junit.framework.Assert.assertTrue; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 28 | import static junit.framework.Assert.fail; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 29 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 30 | import static org.mockito.Matchers.any; |
| 31 | import static org.mockito.Matchers.anyInt; |
| 32 | import static org.mockito.Matchers.anyListOf; |
| 33 | import static org.mockito.Matchers.anyString; |
| 34 | import static org.mockito.Matchers.eq; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 35 | import static org.mockito.Matchers.isA; |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 36 | import static org.mockito.Mockito.atLeastOnce; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 37 | import static org.mockito.Mockito.doAnswer; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.doThrow; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 39 | import static org.mockito.Mockito.mock; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 40 | import static org.mockito.Mockito.times; |
| 41 | import static org.mockito.Mockito.verify; |
| 42 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 43 | import static org.mockito.Mockito.verifyZeroInteractions; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 44 | import static org.mockito.Mockito.when; |
| 45 | |
| 46 | import android.Manifest.permission; |
| 47 | import android.content.ComponentName; |
| 48 | import android.content.ContentResolver; |
| 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.content.ServiceConnection; |
| 52 | import android.content.pm.PackageManager; |
| 53 | import android.content.res.Resources; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 54 | import android.net.INetworkRecommendationProvider; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 55 | import android.net.INetworkScoreCache; |
| 56 | import android.net.NetworkKey; |
Jeremy Joslin | b17a5ed | 2016-12-29 15:00:23 -0800 | [diff] [blame] | 57 | import android.net.NetworkScoreManager; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 58 | import android.net.NetworkScorerAppManager; |
| 59 | import android.net.NetworkScorerAppManager.NetworkScorerAppData; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 60 | import android.net.RecommendationRequest; |
| 61 | import android.net.RecommendationResult; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 62 | import android.net.ScoredNetwork; |
| 63 | import android.net.WifiKey; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 64 | import android.net.wifi.WifiConfiguration; |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 65 | import android.os.Binder; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 66 | import android.os.Bundle; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 67 | import android.os.IBinder; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 68 | import android.os.IRemoteCallback; |
| 69 | import android.os.Looper; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 70 | import android.os.RemoteException; |
| 71 | import android.os.UserHandle; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 72 | import android.support.test.InstrumentationRegistry; |
| 73 | import android.support.test.filters.MediumTest; |
| 74 | import android.support.test.runner.AndroidJUnit4; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 75 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 76 | import com.android.server.devicepolicy.MockUtils; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 77 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 78 | import org.junit.Before; |
| 79 | import org.junit.Test; |
| 80 | import org.junit.runner.RunWith; |
| 81 | import org.mockito.ArgumentCaptor; |
| 82 | import org.mockito.Captor; |
| 83 | import org.mockito.Mock; |
| 84 | import org.mockito.MockitoAnnotations; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 85 | import org.mockito.invocation.InvocationOnMock; |
| 86 | import org.mockito.stubbing.Answer; |
| 87 | |
| 88 | import java.io.FileDescriptor; |
| 89 | import java.io.PrintWriter; |
| 90 | import java.io.StringWriter; |
| 91 | import java.util.List; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Tests for {@link NetworkScoreService}. |
| 95 | */ |
| 96 | @RunWith(AndroidJUnit4.class) |
| 97 | @MediumTest |
| 98 | public class NetworkScoreServiceTest { |
| 99 | private static final ScoredNetwork SCORED_NETWORK = |
| 100 | new ScoredNetwork(new NetworkKey(new WifiKey("\"ssid\"", "00:00:00:00:00:00")), |
| 101 | null /* rssiCurve*/); |
Jeremy Joslin | b8418ac8 | 2016-12-06 07:42:38 -0800 | [diff] [blame] | 102 | private static final NetworkScorerAppData NEW_SCORER = |
| 103 | new NetworkScorerAppData("newPackageName", 1, "newScoringServiceClass"); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 104 | |
| 105 | @Mock private PackageManager mPackageManager; |
| 106 | @Mock private NetworkScorerAppManager mNetworkScorerAppManager; |
| 107 | @Mock private Context mContext; |
| 108 | @Mock private Resources mResources; |
| 109 | @Mock private INetworkScoreCache.Stub mNetworkScoreCache, mNetworkScoreCache2; |
| 110 | @Mock private IBinder mIBinder, mIBinder2; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 111 | @Mock private INetworkRecommendationProvider mRecommendationProvider; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 112 | @Captor private ArgumentCaptor<List<ScoredNetwork>> mScoredNetworkCaptor; |
| 113 | |
| 114 | private ContentResolver mContentResolver; |
| 115 | private NetworkScoreService mNetworkScoreService; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 116 | private RecommendationRequest mRecommendationRequest; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 117 | |
| 118 | @Before |
| 119 | public void setUp() throws Exception { |
| 120 | MockitoAnnotations.initMocks(this); |
| 121 | when(mNetworkScoreCache.asBinder()).thenReturn(mIBinder); |
| 122 | when(mNetworkScoreCache2.asBinder()).thenReturn(mIBinder2); |
| 123 | mContentResolver = InstrumentationRegistry.getContext().getContentResolver(); |
| 124 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
| 125 | when(mContext.getResources()).thenReturn(mResources); |
| 126 | mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager); |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 127 | WifiConfiguration configuration = new WifiConfiguration(); |
Jeremy Joslin | 29ed4a9 | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 128 | configuration.SSID = "NetworkScoreServiceTest_SSID"; |
| 129 | configuration.BSSID = "NetworkScoreServiceTest_BSSID"; |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 130 | mRecommendationRequest = new RecommendationRequest.Builder() |
| 131 | .setCurrentRecommendedWifiConfig(configuration).build(); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 135 | public void testSystemRunning() { |
| 136 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 137 | |
| 138 | mNetworkScoreService.systemRunning(); |
| 139 | |
Jeremy Joslin | b17a5ed | 2016-12-29 15:00:23 -0800 | [diff] [blame] | 140 | verify(mContext).bindServiceAsUser(MockUtils.checkIntent( |
| 141 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 142 | .setComponent(new ComponentName(NEW_SCORER.packageName, |
| 143 | NEW_SCORER.recommendationServiceClassName))), |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 144 | any(ServiceConnection.class), |
| 145 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 146 | eq(UserHandle.SYSTEM)); |
| 147 | } |
| 148 | |
| 149 | @Test |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 150 | public void testRequestScores_noPermission() throws Exception { |
| 151 | doThrow(new SecurityException()).when(mContext) |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 152 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 153 | anyString()); |
| 154 | try { |
| 155 | mNetworkScoreService.requestScores(null); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 156 | fail("REQUEST_NETWORK_SCORES not enforced."); |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 157 | } catch (SecurityException e) { |
| 158 | // expected |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testRequestScores_providerNotConnected() throws Exception { |
| 164 | assertFalse(mNetworkScoreService.requestScores(new NetworkKey[0])); |
| 165 | verifyZeroInteractions(mRecommendationProvider); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testRequestScores_providerThrowsRemoteException() throws Exception { |
| 170 | injectProvider(); |
| 171 | doThrow(new RemoteException()).when(mRecommendationProvider) |
| 172 | .requestScores(any(NetworkKey[].class)); |
| 173 | |
| 174 | assertFalse(mNetworkScoreService.requestScores(new NetworkKey[0])); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testRequestScores_providerAvailable() throws Exception { |
| 179 | injectProvider(); |
| 180 | |
| 181 | final NetworkKey[] networks = new NetworkKey[0]; |
| 182 | assertTrue(mNetworkScoreService.requestScores(networks)); |
| 183 | verify(mRecommendationProvider).requestScores(networks); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testRequestRecommendation_noPermission() throws Exception { |
| 188 | doThrow(new SecurityException()).when(mContext) |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 189 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 190 | anyString()); |
| 191 | try { |
| 192 | mNetworkScoreService.requestRecommendation(mRecommendationRequest); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 193 | fail("REQUEST_NETWORK_SCORES not enforced."); |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 194 | } catch (SecurityException e) { |
| 195 | // expected |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void testRequestRecommendation_mainThread() throws Exception { |
| 201 | when(mContext.getMainLooper()).thenReturn(Looper.myLooper()); |
| 202 | try { |
| 203 | mNetworkScoreService.requestRecommendation(mRecommendationRequest); |
| 204 | fail("requestRecommendation run on main thread."); |
| 205 | } catch (RuntimeException e) { |
| 206 | // expected |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testRequestRecommendation_providerNotConnected() throws Exception { |
| 212 | when(mContext.getMainLooper()).thenReturn(Looper.getMainLooper()); |
| 213 | |
| 214 | final RecommendationResult result = |
| 215 | mNetworkScoreService.requestRecommendation(mRecommendationRequest); |
| 216 | assertNotNull(result); |
| 217 | assertEquals(mRecommendationRequest.getCurrentSelectedConfig(), |
| 218 | result.getWifiConfiguration()); |
| 219 | } |
| 220 | |
| 221 | @Test |
| 222 | public void testRequestRecommendation_providerThrowsRemoteException() throws Exception { |
| 223 | injectProvider(); |
| 224 | when(mContext.getMainLooper()).thenReturn(Looper.getMainLooper()); |
| 225 | doThrow(new RemoteException()).when(mRecommendationProvider) |
| 226 | .requestRecommendation(eq(mRecommendationRequest), isA(IRemoteCallback.class), |
| 227 | anyInt()); |
| 228 | |
| 229 | final RecommendationResult result = |
| 230 | mNetworkScoreService.requestRecommendation(mRecommendationRequest); |
| 231 | assertNotNull(result); |
| 232 | assertEquals(mRecommendationRequest.getCurrentSelectedConfig(), |
| 233 | result.getWifiConfiguration()); |
| 234 | } |
| 235 | |
| 236 | @Test |
| 237 | public void testRequestRecommendation_resultReturned() throws Exception { |
| 238 | injectProvider(); |
| 239 | when(mContext.getMainLooper()).thenReturn(Looper.getMainLooper()); |
| 240 | final WifiConfiguration wifiConfiguration = new WifiConfiguration(); |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 241 | wifiConfiguration.SSID = "testRequestRecommendation_resultReturned_SSID"; |
| 242 | wifiConfiguration.BSSID = "testRequestRecommendation_resultReturned_BSSID"; |
| 243 | final RecommendationResult providerResult = RecommendationResult |
| 244 | .createConnectRecommendation(wifiConfiguration); |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 245 | final Bundle bundle = new Bundle(); |
| 246 | bundle.putParcelable(EXTRA_RECOMMENDATION_RESULT, providerResult); |
| 247 | doAnswer(invocation -> { |
| 248 | bundle.putInt(EXTRA_SEQUENCE, invocation.getArgumentAt(2, int.class)); |
| 249 | invocation.getArgumentAt(1, IRemoteCallback.class).sendResult(bundle); |
| 250 | return null; |
| 251 | }).when(mRecommendationProvider) |
| 252 | .requestRecommendation(eq(mRecommendationRequest), isA(IRemoteCallback.class), |
| 253 | anyInt()); |
| 254 | |
| 255 | final RecommendationResult result = |
| 256 | mNetworkScoreService.requestRecommendation(mRecommendationRequest); |
| 257 | assertNotNull(result); |
| 258 | assertEquals(providerResult.getWifiConfiguration().SSID, |
| 259 | result.getWifiConfiguration().SSID); |
Amin Shaikh | 3d18c62 | 2016-12-09 17:11:50 -0800 | [diff] [blame] | 260 | assertEquals(providerResult.getWifiConfiguration().BSSID, |
| 261 | result.getWifiConfiguration().BSSID); |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Test |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 265 | public void testUpdateScores_notActiveScorer() { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 266 | bindToScorer(false /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 267 | |
| 268 | try { |
| 269 | mNetworkScoreService.updateScores(new ScoredNetwork[0]); |
| 270 | fail("SecurityException expected"); |
| 271 | } catch (SecurityException e) { |
| 272 | // expected |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | @Test |
| 277 | public void testUpdateScores_oneRegisteredCache() throws RemoteException { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 278 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 279 | |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 280 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, |
| 281 | mNetworkScoreCache, CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 282 | |
| 283 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 284 | |
| 285 | verify(mNetworkScoreCache).updateScores(mScoredNetworkCaptor.capture()); |
| 286 | |
| 287 | assertEquals(1, mScoredNetworkCaptor.getValue().size()); |
| 288 | assertEquals(SCORED_NETWORK, mScoredNetworkCaptor.getValue().get(0)); |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testUpdateScores_twoRegisteredCaches() throws RemoteException { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 293 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 294 | |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 295 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, |
| 296 | mNetworkScoreCache, CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 297 | mNetworkScoreService.registerNetworkScoreCache( |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 298 | NetworkKey.TYPE_WIFI, mNetworkScoreCache2, CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 299 | |
| 300 | // updateScores should update both caches |
| 301 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 302 | |
| 303 | verify(mNetworkScoreCache).updateScores(anyListOf(ScoredNetwork.class)); |
| 304 | verify(mNetworkScoreCache2).updateScores(anyListOf(ScoredNetwork.class)); |
| 305 | |
| 306 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 307 | NetworkKey.TYPE_WIFI, mNetworkScoreCache2); |
| 308 | |
| 309 | // updateScores should only update the first cache since the 2nd has been unregistered |
| 310 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 311 | |
| 312 | verify(mNetworkScoreCache, times(2)).updateScores(anyListOf(ScoredNetwork.class)); |
| 313 | |
| 314 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 315 | NetworkKey.TYPE_WIFI, mNetworkScoreCache); |
| 316 | |
| 317 | // updateScores should not update any caches since they are both unregistered |
| 318 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 319 | |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 320 | // The register and unregister calls grab the binder from the score cache. |
| 321 | verify(mNetworkScoreCache, atLeastOnce()).asBinder(); |
| 322 | verify(mNetworkScoreCache2, atLeastOnce()).asBinder(); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 323 | verifyNoMoreInteractions(mNetworkScoreCache, mNetworkScoreCache2); |
| 324 | } |
| 325 | |
| 326 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 327 | public void testClearScores_notActiveScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 328 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 329 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 330 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 331 | try { |
| 332 | mNetworkScoreService.clearScores(); |
| 333 | fail("SecurityException expected"); |
| 334 | } catch (SecurityException e) { |
| 335 | // expected |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 340 | public void testClearScores_activeScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 341 | bindToScorer(true /*callerIsScorer*/); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 342 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 343 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 344 | |
| 345 | mNetworkScoreService.clearScores(); |
| 346 | } |
| 347 | |
| 348 | @Test |
| 349 | public void testClearScores_activeScorer() throws RemoteException { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 350 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 351 | |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 352 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache, |
| 353 | CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 354 | mNetworkScoreService.clearScores(); |
| 355 | |
| 356 | verify(mNetworkScoreCache).clearScores(); |
| 357 | } |
| 358 | |
| 359 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 360 | public void testClearScores_notActiveScorer_hasRequestNetworkScoresPermission() |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 361 | throws RemoteException { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 362 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 363 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 364 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 365 | |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 366 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache, |
| 367 | CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 368 | mNetworkScoreService.clearScores(); |
| 369 | |
| 370 | verify(mNetworkScoreCache).clearScores(); |
| 371 | } |
| 372 | |
| 373 | @Test |
| 374 | public void testSetActiveScorer_noScoreNetworksPermission() { |
| 375 | doThrow(new SecurityException()).when(mContext) |
| 376 | .enforceCallingOrSelfPermission(eq(permission.SCORE_NETWORKS), anyString()); |
| 377 | |
| 378 | try { |
| 379 | mNetworkScoreService.setActiveScorer(null); |
| 380 | fail("SecurityException expected"); |
| 381 | } catch (SecurityException e) { |
| 382 | // expected |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 387 | public void testDisableScoring_notActiveScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 388 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 389 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 390 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 391 | |
| 392 | try { |
| 393 | mNetworkScoreService.disableScoring(); |
| 394 | fail("SecurityException expected"); |
| 395 | } catch (SecurityException e) { |
| 396 | // expected |
| 397 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 401 | public void testRegisterNetworkScoreCache_noRequestNetworkScoresPermission() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 402 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 403 | eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 404 | |
| 405 | try { |
| 406 | mNetworkScoreService.registerNetworkScoreCache( |
Jeremy Joslin | 823db05 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 407 | NetworkKey.TYPE_WIFI, mNetworkScoreCache, CACHE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 408 | fail("SecurityException expected"); |
| 409 | } catch (SecurityException e) { |
| 410 | // expected |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | @Test |
Jeremy Joslin | 23f1fcd | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 415 | public void testUnregisterNetworkScoreCache_noRequestNetworkScoresPermission() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 416 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Jeremy Joslin | 186f333 | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 417 | eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 418 | |
| 419 | try { |
| 420 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 421 | NetworkKey.TYPE_WIFI, mNetworkScoreCache); |
| 422 | fail("SecurityException expected"); |
| 423 | } catch (SecurityException e) { |
| 424 | // expected |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | @Test |
| 429 | public void testDump_noDumpPermission() { |
| 430 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
| 431 | eq(permission.DUMP), anyString()); |
| 432 | |
| 433 | try { |
| 434 | mNetworkScoreService.dump( |
| 435 | new FileDescriptor(), new PrintWriter(new StringWriter()), new String[0]); |
| 436 | fail("SecurityException expected"); |
| 437 | } catch (SecurityException e) { |
| 438 | // expected |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | @Test |
| 443 | public void testDump_doesNotCrash() { |
| 444 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 445 | StringWriter stringWriter = new StringWriter(); |
| 446 | |
| 447 | mNetworkScoreService.dump( |
| 448 | new FileDescriptor(), new PrintWriter(stringWriter), new String[0]); |
| 449 | |
| 450 | assertFalse(stringWriter.toString().isEmpty()); |
| 451 | } |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 452 | |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 453 | @Test |
| 454 | public void testIsCallerActiveScorer_noBoundService() throws Exception { |
| 455 | mNetworkScoreService.systemRunning(); |
| 456 | |
| 457 | assertFalse(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); |
| 458 | } |
| 459 | |
| 460 | @Test |
| 461 | public void testIsCallerActiveScorer_boundServiceIsNotCaller() throws Exception { |
| 462 | bindToScorer(false /*callerIsScorer*/); |
| 463 | |
| 464 | assertFalse(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); |
| 465 | } |
| 466 | |
| 467 | @Test |
| 468 | public void testIsCallerActiveScorer_boundServiceIsCaller() throws Exception { |
| 469 | bindToScorer(true /*callerIsScorer*/); |
| 470 | |
| 471 | assertTrue(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); |
| 472 | } |
| 473 | |
Jeremy Joslin | d7670f6 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 474 | @Test |
| 475 | public void testGetActiveScorerPackage_notActive() throws Exception { |
| 476 | mNetworkScoreService.systemRunning(); |
| 477 | |
| 478 | assertNull(mNetworkScoreService.getActiveScorerPackage()); |
| 479 | } |
| 480 | |
| 481 | @Test |
| 482 | public void testGetActiveScorerPackage_active() throws Exception { |
| 483 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 484 | mNetworkScoreService.systemRunning(); |
| 485 | |
| 486 | assertEquals(NEW_SCORER.packageName, mNetworkScoreService.getActiveScorerPackage()); |
| 487 | } |
| 488 | |
Jeremy Joslin | 36d4c48 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 489 | // "injects" the mock INetworkRecommendationProvider into the NetworkScoreService. |
| 490 | private void injectProvider() { |
| 491 | final ComponentName componentName = new ComponentName(NEW_SCORER.packageName, |
| 492 | NEW_SCORER.recommendationServiceClassName); |
| 493 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 494 | when(mContext.bindServiceAsUser(isA(Intent.class), isA(ServiceConnection.class), anyInt(), |
| 495 | isA(UserHandle.class))).thenAnswer(new Answer<Boolean>() { |
| 496 | @Override |
| 497 | public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 498 | IBinder mockBinder = mock(IBinder.class); |
| 499 | when(mockBinder.queryLocalInterface(anyString())) |
| 500 | .thenReturn(mRecommendationProvider); |
| 501 | invocation.getArgumentAt(1, ServiceConnection.class) |
| 502 | .onServiceConnected(componentName, mockBinder); |
| 503 | return true; |
| 504 | } |
| 505 | }); |
| 506 | mNetworkScoreService.systemRunning(); |
| 507 | } |
Jeremy Joslin | 9b442fa | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 508 | |
| 509 | private void bindToScorer(boolean callerIsScorer) { |
| 510 | final int callingUid = callerIsScorer ? Binder.getCallingUid() : 0; |
| 511 | NetworkScorerAppData appData = new NetworkScorerAppData(NEW_SCORER.packageName, |
| 512 | callingUid, NEW_SCORER.recommendationServiceClassName); |
| 513 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(appData); |
| 514 | when(mContext.bindServiceAsUser(isA(Intent.class), isA(ServiceConnection.class), anyInt(), |
| 515 | isA(UserHandle.class))).thenReturn(true); |
| 516 | mNetworkScoreService.systemRunning(); |
| 517 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 518 | } |