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

Commit a6d7c4f8 authored by Jack Yu's avatar Jack Yu Committed by Gerrit Code Review
Browse files

Merge changes Icfb050d8,I678d5d8b

* changes:
  Prevent VVM from sending SMS in ECBM
  Added unit tests for FDN changes.
parents 844817ae fcc7b2e5
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.telephony.SubscriptionManager;
import android.telephony.TelephonyFrameworkInitializer;
import android.telephony.TelephonyManager;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.telephony.Rlog;

@@ -57,7 +58,8 @@ public class SmsController extends ISmsImplBase {

    private final Context mContext;

    protected SmsController(Context context) {
    @VisibleForTesting
    public SmsController(Context context) {
        mContext = context;
        ServiceRegisterer smsServiceRegisterer = TelephonyFrameworkInitializer
                .getTelephonyServiceManager()
@@ -751,6 +753,13 @@ public class SmsController extends ISmsImplBase {
    public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
            String callingAttributionTag, int subId, String number, int port, String text,
            PendingIntent sentIntent) {
        // Do not send non-emergency SMS in ECBM as it forces device to exit ECBM.
        if(getPhone(subId).isInEcm()) {
            Rlog.d(LOG_TAG, "sendVisualVoicemailSmsForSubscriber: Do not send non-emergency "
                + "SMS in ECBM as it forces device to exit ECBM.");
            return;
        }

        if (port == 0) {
            sendTextForSubscriberWithSelfPermissionsInternal(subId, callingPackage,
                    callingAttributionTag, number, null, text, sentIntent, null, false,
@@ -900,7 +909,8 @@ public class SmsController extends ISmsImplBase {
     * @param destAddr destination address of the message
     * @return true if either destAddr or smscAddr is blocked due to FDN.
     */
    private boolean isNumberBlockedByFDN(int subId, String destAddr, String callingPackage) {
    @VisibleForTesting
    public boolean isNumberBlockedByFDN(int subId, String destAddr, String callingPackage) {
        int phoneId = SubscriptionManager.getPhoneId(subId);
        if (!FdnUtils.isFdnEnabled(phoneId)) {
            return false;
+37 −0
Original line number Diff line number Diff line
@@ -2040,6 +2040,43 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
        fdnCheckCleanup();
    }

    @Test
    public void testDial_fdnCheck() throws Exception{
        // dial setup
        mSST.mSS = mServiceState;
        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
        mCT.mForegroundCall = mGsmCdmaCall;
        mCT.mBackgroundCall = mGsmCdmaCall;
        mCT.mRingingCall = mGsmCdmaCall;
        doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
        replaceInstance(Phone.class, "mImsPhone", mPhoneUT, mImsPhone);

        // FDN check setup
        fdnCheckSetup();
        ArrayList<AdnRecord> fdnList = new ArrayList<>();
        doReturn(fdnList).when(adnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);

        // FDN check success - no exception is returned
        AdnRecord dialRecord = new AdnRecord(null, "1234567890");
        fdnList.add(0, dialRecord);
        Connection connection = mPhoneUT.dial("1234567890",
                new PhoneInternalInterface.DialArgs.Builder().build());
        verify(mCT).dialGsm(eq("1234567890"), any(PhoneInternalInterface.DialArgs.class));

        // FDN check failure - returns CallStateException
        fdnList.remove(0);
        try {
            connection = mPhoneUT.dial("1234567890",
                    new PhoneInternalInterface.DialArgs.Builder().build());
            fail("Expected CallStateException with ERROR_FDN_BLOCKED thrown.");
        } catch(CallStateException e) {
            assertEquals(CallStateException.ERROR_FDN_BLOCKED, e.getError());
        }

        // clean up
        fdnCheckCleanup();
    }

    public void fdnCheckCleanup() {
        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnAvailable();
        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
+195 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony;

import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import com.android.internal.telephony.uicc.AdnRecord;
import com.android.internal.telephony.uicc.AdnRecordCache;
import com.android.internal.telephony.uicc.IccConstants;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;


import java.util.ArrayList;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
public class SmsControllerTest extends TelephonyTest {

    // Mocked classes
    private AdnRecordCache mAdnRecordCache;

    // SmsController under test
    private SmsController mSmsControllerUT;
    private final String smscAddrStr = "+1206313004";
    private String mCallingPackage;

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
        mAdnRecordCache = Mockito.mock(AdnRecordCache.class);
        mSmsControllerUT = new SmsController(mContext);
        mCallingPackage = mContext.getOpPackageName();
    }

    @After
    public void tearDown() throws Exception {
        mAdnRecordCache = null;
        super.tearDown();
    }

    private void fdnCheckSetup() {
        // FDN check setup
        doReturn(mAdnRecordCache).when(mSimRecords).getAdnCache();
        doReturn(mUiccProfile).when(mUiccController).getUiccProfileForPhone(anyInt());
        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnAvailable();
        doReturn(true).when(mUiccCardApplication3gpp).getIccFdnEnabled();
        doReturn(false).when(mTelephonyManager).isEmergencyNumber(anyString());
        doReturn("us").when(mTelephonyManager).getSimCountryIso();
        doReturn(smscAddrStr).when(mIccSmsInterfaceManager).getSmscAddressFromIccEf(anyString());
    }

    private void fdnCheckCleanup() {
        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnAvailable();
        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
    }

    @Test
    public void isNumberBlockedByFdn_fdnListHasBothDestAddrAndSmscAddr() {
        // FDN check setup
        fdnCheckSetup();
        ArrayList<AdnRecord> fdnList = new ArrayList<>();
        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);

        // FDN list has both destination addr and smsc addr
        AdnRecord smscAddrRecord = new AdnRecord(null, smscAddrStr);
        AdnRecord destAddrRecord = new AdnRecord(null, "1234");
        fdnList.add(0, smscAddrRecord);
        fdnList.add(1, destAddrRecord);

        // Returns false as list contains both dest addr and smsc addr
        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
                mCallingPackage));

        // Clean up
        fdnCheckCleanup();
    }

    @Test
    public void isNumberBlockedByFdn_fdnListHasDestAddr() {
        // FDN check setup
        fdnCheckSetup();
        ArrayList<AdnRecord> fdnList = new ArrayList<>();
        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);

        // FDN list has only destination addr
        AdnRecord destAddrRecord = new AdnRecord(null, "1234");
        fdnList.add(0, destAddrRecord);

        // Returns true as list does not contain smsc addr
        assertTrue(mSmsControllerUT.isNumberBlockedByFDN(1, "1234", mCallingPackage));

        // Clean up
        fdnCheckCleanup();
    }

    @Test
    public void isNumberBlockedByFdn_fdnListHasSmscAddr() {
        // FDN check setup
        fdnCheckSetup();
        ArrayList<AdnRecord> fdnList = new ArrayList<>();
        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);

        // FDN list has both destination addr and smsc addr
        AdnRecord smscAddrRecord = new AdnRecord(null, smscAddrStr);
        fdnList.add(0, smscAddrRecord);

        // Returns true as list does not contain dest addr
        assertTrue(mSmsControllerUT.isNumberBlockedByFDN(1, "1234", mCallingPackage));

        // Clean up
        fdnCheckCleanup();
    }

    @Test
    public void isNumberBlockedByFdn_destAddrIsEmergencyNumber() {
        // FDN check setup
        fdnCheckSetup();
        ArrayList<AdnRecord> fdnList = new ArrayList<>();
        doReturn(fdnList).when(mAdnRecordCache).getRecordsIfLoaded(IccConstants.EF_FDN);

        doReturn(true).when(mTelephonyManager).isEmergencyNumber(anyString());
        // Returns false as dest addr is emergency number
        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
                mCallingPackage));

        // Clean up
        fdnCheckCleanup();
    }

    @Test
    public void isNumberBlockedByFdn_fdnDisabled() {
        // FDN check setup
        fdnCheckSetup();

        doReturn(false).when(mUiccCardApplication3gpp).getIccFdnEnabled();
        // Returns false as fdn is not enabled
        assertFalse(mSmsControllerUT.isNumberBlockedByFDN(1, "1234",
                mCallingPackage));

        // Clean up
        fdnCheckCleanup();
    }

    @Test
    public void sendVisualVoicemailSmsForSubscriber_phoneIsNotInEcm() {
        assertFalse(mPhone.isInEcm());

        mSmsControllerUT.sendVisualVoicemailSmsForSubscriber(mCallingPackage,null ,
                1, null, 0, null, null);
        verify(mIccSmsInterfaceManager).sendTextWithSelfPermissions(any(),
                any(), any(), any(), any(), any(), any(), eq(false), eq(true));
    }

    @Test
    public void sendVisualVoicemailSmsForSubscriber_phoneIsInEcm() {
        doReturn(true).when(mPhone).isInEcm();

        mSmsControllerUT.sendVisualVoicemailSmsForSubscriber(mCallingPackage,null ,
                1, null, 0, null, null);
        verify(mIccSmsInterfaceManager, never()).sendTextWithSelfPermissions(any(),
                any(), any(), any(), any(), any(), any(), eq(false), eq(true));

        doReturn(false).when(mPhone).isInEcm();
    }
}
 No newline at end of file