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

Commit 919113a2 authored by fionaxu's avatar fionaxu
Browse files

handle invalid SMS destination address gracefully

Bug: 37578305
Test: runtest --path
frameworks/opt/telephony/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java
--test-method testSendTextWithInvalidDestAddr
Change-Id: I4a93052b480e8e6f6c979506972e61753de9d894
parent 327d466a
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -16,13 +16,20 @@

package com.android.internal.telephony.gsm;

import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Country;
import android.location.CountryDetector;
import android.os.HandlerThread;
@@ -30,13 +37,16 @@ import android.os.Message;
import android.os.SystemProperties;
import android.provider.Telephony;
import android.support.test.filters.FlakyTest;
import android.telephony.SmsManager;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Singleton;

import com.android.internal.telephony.ISub;
import com.android.internal.telephony.ImsSMSDispatcher;
import com.android.internal.telephony.TelephonyTest;
import com.android.internal.telephony.TelephonyTestUtils;
import com.android.internal.telephony.TestApplication;

import org.junit.After;
import org.junit.Before;
@@ -56,6 +66,18 @@ public class GsmSmsDispatcherTest extends TelephonyTest {
    private CountryDetector mCountryDetector;
    @Mock
    private ISub.Stub mISubStub;
    private Object mLock = new Object();
    private boolean mReceivedTestIntent = false;
    private static final String TEST_INTENT = "com.android.internal.telephony.TEST_INTENT";
    private BroadcastReceiver mTestReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            logd("onReceive");
            synchronized (mLock) {
                mReceivedTestIntent = true;
            }
        }
    };

    private GsmSMSDispatcher mGsmSmsDispatcher;
    private GsmSmsDispatcherTestHandler mGsmSmsDispatcherTestHandler;
@@ -147,4 +169,26 @@ public class GsmSmsDispatcherTest extends TelephonyTest {
            return systemEmergencyNumbers.split(",")[0];
        }
    }

    @Test @SmallTest
    public void testSendTextWithInvalidDestAddr() throws Exception {
        // unmock ActivityManager to be able to register receiver, create real PendingIntent and
        // receive TEST_INTENT
        restoreInstance(Singleton.class, "mInstance", mIActivityManagerSingleton);
        restoreInstance(ActivityManager.class, "IActivityManagerSingleton", null);
        Context realContext = TestApplication.getAppContext();
        realContext.registerReceiver(mTestReceiver, new IntentFilter(TEST_INTENT));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(realContext, 0,
                new Intent(TEST_INTENT), 0);
        // send invalid dest address: +
        mGsmSmsDispatcher.sendText("+", "222" /*scAddr*/, TAG,
                pendingIntent, null, null, null, false);
        waitForMs(500);
        verify(mSimulatedCommandsVerifier, times(0)).sendSMS(anyString(), anyString(),
                any(Message.class));
        synchronized (mLock) {
            assertEquals(true, mReceivedTestIntent);
            assertEquals(SmsManager.RESULT_ERROR_NULL_PDU, mTestReceiver.getResultCode());
        }
    }
}