Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9bb565e4 authored by rambowang's avatar rambowang Committed by Rambo Wang
Browse files

Make NetworkScanRequestTracker.NetworkScanRequestInfo @VisibleForTesting

Add test case to make sure that if the NetworkScanRequestInfo from the
ScanResult is not identical (value equality is not enough) to live request, the ScanResult will be ignored.

Bug: 282921687
Bug: 283308895
Test: atest NetworkScanRequestTrackerTest
Change-Id: I082adb4eca30ef8f2e4deb832f59717972dee296
parent 4200adca
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.telephony.SubscriptionInfo;
import android.telephony.TelephonyScanManager;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.subscription.SubscriptionManagerService;
import com.android.internal.util.ArrayUtils;

@@ -252,9 +253,10 @@ public final class NetworkScanRequestTracker {
    /**
    * Tracks info about the radio network scan.
     *
    * Also used to notice when the calling process dies so we can self-expire.
    * Also used to notice when the calling process dies, so we can self-expire.
    */
    class NetworkScanRequestInfo implements IBinder.DeathRecipient {
    @VisibleForTesting
    public class NetworkScanRequestInfo implements IBinder.DeathRecipient {
        private final NetworkScanRequest mRequest;
        private final Messenger mMessenger;
        private final IBinder mBinder;
@@ -266,8 +268,9 @@ public final class NetworkScanRequestTracker {
        private final String mCallingPackage;
        private boolean mIsBinderDead;

        NetworkScanRequestInfo(NetworkScanRequest r, Messenger m, IBinder b, int id, Phone phone,
                int callingUid, int callingPid, String callingPackage,
        @VisibleForTesting
        public NetworkScanRequestInfo(NetworkScanRequest r, Messenger m, IBinder b, int id,
                Phone phone, int callingUid, int callingPid, String callingPackage,
                boolean renounceFineLocationAccess) {
            super();
            mRequest = r;
+58 −5
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.Log;

import com.android.internal.telephony.NetworkScanRequestTracker.NetworkScanRequestInfo;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -68,7 +70,7 @@ import java.util.concurrent.TimeUnit;
public class NetworkScanRequestTrackerTest extends TelephonyTest {
    private static final String TAG = "NetworkScanRequestTrackerTest";

    private static final String CLIENT_PKG = "com.android.phone";
    private static final String CLIENT_PKG = "com.android.testapp";
    private static final int CLIENT_UID = 123456;

    // Keep the same as in NetworkScanRequestTracker.
@@ -589,6 +591,20 @@ public class NetworkScanRequestTrackerTest extends TelephonyTest {
        );
    }

    @Test
    public void testStartNetworkScan_resultFromCopiedRequest_noMessagesNotified() throws Exception {
        mScanId = scanNetworkWithOneShot(true /* renounceFineLocationAccess */);

        // Scan result came but with copied (instead of original) NetworkScanRequestInfo
        NetworkScanRequestInfo obsoletedNsri = createNetworkScanRequestInfo(mScanId);
        verifyStartNetworkScanAndEmulateScanResult(obsoletedNsri,
                new NetworkScanResult(NetworkScanResult.SCAN_STATUS_COMPLETE,
                        NetworkScan.SUCCESS, List.of(new CellInfoLte())));

        // No message should send to client
        verifyMessages();
    }

    @Test
    public void testStartNetworkScan_multiResultsAndSuccess_allMessagesNotified() throws Exception {
        mMessageLatch = new CountDownLatch(4); // 4 messages expected
@@ -766,12 +782,31 @@ public class NetworkScanRequestTrackerTest extends TelephonyTest {
    /**
     * Verify both {@link CommandsInterface#startNetworkScan(NetworkScanRequest, Message)} and
     * {@link CommandsInterface#registerForNetworkScanResult(Handler, int, Object)} and sends
     * emulated {@link NetworkScanResult} back through the captures.
     * emulated {@link NetworkScanResult} with original NetworkScanRequestInfo back through the
     * captures.
     */
    private void verifyStartNetworkScanAndEmulateScanResult(
            NetworkScanResult... networkScanResults) {
        IBinder.DeathRecipient nsri = verifyStartNetworkScanAndGetNetworkScanInfo();
        NetworkScanRequestInfo nsri = verifyStartNetworkScanAndGetNetworkScanInfo();

        sendEmulatedScanResult(nsri, networkScanResults);
    }

    /**
     * Similar as verifyStartNetworkScanAndEmulateScanResult(NetworkScanResult...) above but
     * allows to set the customized NetworkScanRequestInfo other than original one.
     */
    private void verifyStartNetworkScanAndEmulateScanResult(
            NetworkScanRequestInfo nsri,
            NetworkScanResult... networkScanResults) {
        // Ignore the original NSRI returned
        verifyStartNetworkScanAndGetNetworkScanInfo();

        sendEmulatedScanResult(nsri, networkScanResults);
    }

    private void sendEmulatedScanResult(NetworkScanRequestInfo nsri,
            NetworkScanResult... networkScanResults) {
        ArgumentCaptor<Handler> handlerCaptor = ArgumentCaptor.forClass(Handler.class);
        verify(mPhone.mCi).registerForNetworkScanResult(handlerCaptor.capture(), anyInt(), any());
        Handler handler = handlerCaptor.getValue();
@@ -798,12 +833,12 @@ public class NetworkScanRequestTrackerTest extends TelephonyTest {
     * Verify {@link Phone#startNetworkScan(NetworkScanRequest, Message)} and
     * capture the NetworkScanRequestInfo for further usage.
     */
    private IBinder.DeathRecipient verifyStartNetworkScanAndGetNetworkScanInfo() {
    private NetworkScanRequestInfo verifyStartNetworkScanAndGetNetworkScanInfo() {
        ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
        verify(mPhone).startNetworkScan(any(), messageCaptor.capture());
        Message responseMessage = messageCaptor.getValue();
        // NetworkScanRequestInfo is not public and can only be treated as IBinder.DeathRecipient
        IBinder.DeathRecipient nsri = (IBinder.DeathRecipient) responseMessage.obj;
        NetworkScanRequestInfo nsri = (NetworkScanRequestInfo) responseMessage.obj;
        AsyncResult.forMessage(responseMessage, nsri, null);
        responseMessage.sendToTarget();
        processAllMessages();
@@ -846,6 +881,24 @@ public class NetworkScanRequestTrackerTest extends TelephonyTest {
        return scanId;
    }

    private NetworkScanRequestInfo createNetworkScanRequestInfo(int scanId) {
        RadioAccessSpecifier[] specifiers = new RadioAccessSpecifier[]{
                new RadioAccessSpecifier(AccessNetworkConstants.AccessNetworkType.EUTRAN, null,
                        null)
        };
        NetworkScanRequest request = new NetworkScanRequest(
                NetworkScanRequest.SCAN_TYPE_ONE_SHOT,
                specifiers,
                5 /* searchPeriodicity */,
                60 /* maxSearchTime in seconds */,
                true /* incrementalResults */,
                5 /* incrementalResultsPeriodicity */,
                null /* PLMNs */);
        Messenger messenger = new Messenger(mHandler);
        return mNetworkScanRequestTracker.new NetworkScanRequestInfo(request, messenger, mIBinder,
                scanId, mPhone, CLIENT_UID, -1, CLIENT_PKG, true);
    }

    private void setHasLocationPermissions(boolean hasPermission) {
        if (!hasPermission) {
            // System location off, LocationAccessPolicy#checkLocationPermission returns DENIED_SOFT