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

Commit 391f0edf authored by Chen Xu's avatar Chen Xu Committed by Android (Google) Code Review
Browse files

Merge "handle invalid SMS destination address gracefully" into oc-dr1-dev

parents a7d94e63 919113a2
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());
        }
    }
}