Merge "Moved isCallerActiveScorer() to the score service." am: 663b444196 am: 5fbd2791ac

am: 11b1311e21

Change-Id: I9e00b4593005729cdf2b01e2347f0513a6d52cf0
This commit is contained in:
Jeremy Joslin
2017-01-11 01:50:13 +00:00
committed by android-build-merger
5 changed files with 88 additions and 14 deletions

View File

@@ -100,4 +100,13 @@ interface INetworkScoreService
* @hide * @hide
*/ */
boolean requestScores(in NetworkKey[] networks); boolean requestScores(in NetworkKey[] networks);
/**
* Determine whether the application with the given UID is the enabled scorer.
*
* @param callingUid the UID to check
* @return true if the provided UID is the active scorer, false otherwise.
* @hide
*/
boolean isCallerActiveScorer(int callingUid);
} }

View File

@@ -341,4 +341,19 @@ public class NetworkScoreManager {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
} }
/**
* Determine whether the application with the given UID is the enabled scorer.
*
* @param callingUid the UID to check
* @return true if the provided UID is the active scorer, false otherwise.
* @hide
*/
public boolean isCallerActiveScorer(int callingUid) {
try {
return mService.isCallerActiveScorer(callingUid);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
} }

View File

@@ -16,7 +16,6 @@
package android.net; package android.net;
import android.Manifest;
import android.Manifest.permission; import android.Manifest.permission;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -28,7 +27,9 @@ import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.internal.R; import com.android.internal.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -226,6 +227,7 @@ public class NetworkScorerAppManager {
} }
/** Determine whether the application with the given UID is the enabled scorer. */ /** Determine whether the application with the given UID is the enabled scorer. */
@Deprecated // Use NetworkScoreManager.isCallerActiveScorer()
public boolean isCallerActiveScorer(int callingUid) { public boolean isCallerActiveScorer(int callingUid) {
NetworkScorerAppData defaultApp = getActiveScorer(); NetworkScorerAppData defaultApp = getActiveScorer();
if (defaultApp == null) { if (defaultApp == null) {

View File

@@ -301,7 +301,8 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
// If we're not connected at all then create a new connection. // If we're not connected at all then create a new connection.
if (mServiceConnection == null) { if (mServiceConnection == null) {
mServiceConnection = new ScoringServiceConnection(componentName); mServiceConnection = new ScoringServiceConnection(componentName,
scorerData.packageUid);
} }
// Make sure the connection is connected (idempotent) // Make sure the connection is connected (idempotent)
@@ -325,7 +326,7 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
@Override @Override
public boolean updateScores(ScoredNetwork[] networks) { public boolean updateScores(ScoredNetwork[] networks) {
if (!mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid())) { if (!isCallerActiveScorer(getCallingUid())) {
throw new SecurityException("Caller with UID " + getCallingUid() + throw new SecurityException("Caller with UID " + getCallingUid() +
" is not the active scorer."); " is not the active scorer.");
} }
@@ -389,7 +390,7 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
@Override @Override
public boolean clearScores() { public boolean clearScores() {
// Only the active scorer or the system should be allowed to flush all scores. // Only the active scorer or the system should be allowed to flush all scores.
if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || isCallerSystemUid()) { if (isCallerActiveScorer(getCallingUid()) || isCallerSystemUid()) {
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
clearInternal(); clearInternal();
@@ -418,10 +419,23 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
return false; return false;
} }
/**
* Determine whether the application with the given UID is the enabled scorer.
*
* @param callingUid the UID to check
* @return true if the provided UID is the active scorer, false otherwise.
*/
@Override
public boolean isCallerActiveScorer(int callingUid) {
synchronized (mServiceConnectionLock) {
return mServiceConnection != null && mServiceConnection.mScoringAppUid == callingUid;
}
}
@Override @Override
public void disableScoring() { public void disableScoring() {
// Only the active scorer or the system should be allowed to disable scoring. // Only the active scorer or the system should be allowed to disable scoring.
if (mNetworkScorerAppManager.isCallerActiveScorer(getCallingUid()) || isCallerSystemUid()) { if (isCallerActiveScorer(getCallingUid()) || isCallerSystemUid()) {
// no-op for now but we could write to the setting if needed. // no-op for now but we could write to the setting if needed.
} else { } else {
throw new SecurityException( throw new SecurityException(
@@ -623,12 +637,14 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
private static class ScoringServiceConnection implements ServiceConnection { private static class ScoringServiceConnection implements ServiceConnection {
private final ComponentName mComponentName; private final ComponentName mComponentName;
private final int mScoringAppUid;
private volatile boolean mBound = false; private volatile boolean mBound = false;
private volatile boolean mConnected = false; private volatile boolean mConnected = false;
private volatile INetworkRecommendationProvider mRecommendationProvider; private volatile INetworkRecommendationProvider mRecommendationProvider;
ScoringServiceConnection(ComponentName componentName) { ScoringServiceConnection(ComponentName componentName, int scoringAppUid) {
mComponentName = componentName; mComponentName = componentName;
mScoringAppUid = scoringAppUid;
} }
void connect(Context context) { void connect(Context context) {

View File

@@ -61,6 +61,7 @@ import android.net.RecommendationResult;
import android.net.ScoredNetwork; import android.net.ScoredNetwork;
import android.net.WifiKey; import android.net.WifiKey;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.IRemoteCallback; import android.os.IRemoteCallback;
@@ -261,7 +262,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testUpdateScores_notActiveScorer() { public void testUpdateScores_notActiveScorer() {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(false); bindToScorer(false /*callerIsScorer*/);
try { try {
mNetworkScoreService.updateScores(new ScoredNetwork[0]); mNetworkScoreService.updateScores(new ScoredNetwork[0]);
@@ -273,7 +274,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testUpdateScores_oneRegisteredCache() throws RemoteException { public void testUpdateScores_oneRegisteredCache() throws RemoteException {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(true); bindToScorer(true /*callerIsScorer*/);
mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI,
mNetworkScoreCache, CACHE_FILTER_NONE); mNetworkScoreCache, CACHE_FILTER_NONE);
@@ -288,7 +289,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testUpdateScores_twoRegisteredCaches() throws RemoteException { public void testUpdateScores_twoRegisteredCaches() throws RemoteException {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(true); bindToScorer(true /*callerIsScorer*/);
mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI,
mNetworkScoreCache, CACHE_FILTER_NONE); mNetworkScoreCache, CACHE_FILTER_NONE);
@@ -323,7 +324,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testClearScores_notActiveScorer_noRequestNetworkScoresPermission() { public void testClearScores_notActiveScorer_noRequestNetworkScoresPermission() {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(false); bindToScorer(false /*callerIsScorer*/);
when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
.thenReturn(PackageManager.PERMISSION_DENIED); .thenReturn(PackageManager.PERMISSION_DENIED);
try { try {
@@ -336,7 +337,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testClearScores_activeScorer_noRequestNetworkScoresPermission() { public void testClearScores_activeScorer_noRequestNetworkScoresPermission() {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(true); bindToScorer(true /*callerIsScorer*/);
when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
.thenReturn(PackageManager.PERMISSION_DENIED); .thenReturn(PackageManager.PERMISSION_DENIED);
@@ -345,7 +346,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testClearScores_activeScorer() throws RemoteException { public void testClearScores_activeScorer() throws RemoteException {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(true); bindToScorer(true /*callerIsScorer*/);
mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache, mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache,
CACHE_FILTER_NONE); CACHE_FILTER_NONE);
@@ -357,7 +358,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testClearScores_notActiveScorer_hasRequestNetworkScoresPermission() public void testClearScores_notActiveScorer_hasRequestNetworkScoresPermission()
throws RemoteException { throws RemoteException {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(false); bindToScorer(false /*callerIsScorer*/);
when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
.thenReturn(PackageManager.PERMISSION_GRANTED); .thenReturn(PackageManager.PERMISSION_GRANTED);
@@ -383,7 +384,7 @@ public class NetworkScoreServiceTest {
@Test @Test
public void testDisableScoring_notActiveScorer_noRequestNetworkScoresPermission() { public void testDisableScoring_notActiveScorer_noRequestNetworkScoresPermission() {
when(mNetworkScorerAppManager.isCallerActiveScorer(anyInt())).thenReturn(false); bindToScorer(false /*callerIsScorer*/);
when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
.thenReturn(PackageManager.PERMISSION_DENIED); .thenReturn(PackageManager.PERMISSION_DENIED);
@@ -448,6 +449,27 @@ public class NetworkScoreServiceTest {
assertFalse(stringWriter.toString().isEmpty()); assertFalse(stringWriter.toString().isEmpty());
} }
@Test
public void testIsCallerActiveScorer_noBoundService() throws Exception {
mNetworkScoreService.systemRunning();
assertFalse(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid()));
}
@Test
public void testIsCallerActiveScorer_boundServiceIsNotCaller() throws Exception {
bindToScorer(false /*callerIsScorer*/);
assertFalse(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid()));
}
@Test
public void testIsCallerActiveScorer_boundServiceIsCaller() throws Exception {
bindToScorer(true /*callerIsScorer*/);
assertTrue(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid()));
}
// "injects" the mock INetworkRecommendationProvider into the NetworkScoreService. // "injects" the mock INetworkRecommendationProvider into the NetworkScoreService.
private void injectProvider() { private void injectProvider() {
final ComponentName componentName = new ComponentName(NEW_SCORER.packageName, final ComponentName componentName = new ComponentName(NEW_SCORER.packageName,
@@ -467,4 +489,14 @@ public class NetworkScoreServiceTest {
}); });
mNetworkScoreService.systemRunning(); mNetworkScoreService.systemRunning();
} }
private void bindToScorer(boolean callerIsScorer) {
final int callingUid = callerIsScorer ? Binder.getCallingUid() : 0;
NetworkScorerAppData appData = new NetworkScorerAppData(NEW_SCORER.packageName,
callingUid, NEW_SCORER.recommendationServiceClassName);
when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(appData);
when(mContext.bindServiceAsUser(isA(Intent.class), isA(ServiceConnection.class), anyInt(),
isA(UserHandle.class))).thenReturn(true);
mNetworkScoreService.systemRunning();
}
} }