CarrierServicesSmsFilterTest.java revision 311e8950a8e2662422d999c5bda4f90cca7bf39a
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.telephony; 18 19import static org.junit.Assert.assertFalse; 20import static org.junit.Assert.assertTrue; 21import static org.mockito.Matchers.any; 22import static org.mockito.Matchers.anyInt; 23import static org.mockito.Matchers.anyString; 24import static org.mockito.Matchers.eq; 25import static org.mockito.Mockito.doAnswer; 26import static org.mockito.Mockito.timeout; 27import static org.mockito.Mockito.verify; 28import static org.mockito.Mockito.when; 29 30import android.content.ComponentName; 31import android.content.Intent; 32import android.content.pm.PackageManager; 33import android.content.pm.ServiceInfo; 34import android.os.RemoteException; 35import android.service.carrier.CarrierMessagingService; 36import android.service.carrier.ICarrierMessagingCallback; 37import android.service.carrier.ICarrierMessagingService; 38import android.service.carrier.MessagePdu; 39import android.test.suitebuilder.annotation.SmallTest; 40 41import com.android.internal.telephony.uicc.UiccCard; 42 43import org.junit.After; 44import org.junit.Before; 45import org.junit.Test; 46import org.mockito.Mock; 47import org.mockito.invocation.InvocationOnMock; 48import org.mockito.stubbing.Answer; 49 50import java.util.ArrayList; 51import java.util.List; 52 53/** 54 * Tests SMS filtering by carrier services. 55 */ 56public class CarrierServicesSmsFilterTest extends TelephonyTest { 57 private static final byte[] SMS_PDU = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; 58 private static final String CARRIER_APP_PACKAGE_NAME = "com.android.carrier"; 59 private static final String SYSTEM_APP_PACKAGE_NAME = "com.android.system"; 60 61 private CarrierServicesSmsFilter mCarrierServicesSmsFilterUT; 62 @Mock 63 private CarrierServicesSmsFilter.CarrierServicesSmsFilterCallbackInterface mFilterCallback; 64 @Mock 65 private UiccCard mUiccCard; 66 @Mock 67 private ICarrierMessagingService.Stub mICarrierAppMessagingService; 68 @Mock 69 private ICarrierMessagingService.Stub mISystemCarrierMessagingService; 70 71 @Before 72 public void setUp() throws Exception { 73 super.setUp(getClass().getSimpleName()); 74 mCarrierServicesSmsFilterUT = new CarrierServicesSmsFilter( 75 mContext, mPhone.getPhoneId(), mPhone.getSubId(), new byte[][]{SMS_PDU}, 76 0, null, mFilterCallback, getClass().getSimpleName()); 77 } 78 79 @After 80 public void tearDown() throws Exception { 81 super.tearDown(); 82 } 83 84 @Test 85 @SmallTest 86 public void testFilter_noCarrierServicesFilter_notHandled() throws Exception { 87 assertFalse(mCarrierServicesSmsFilterUT.filter()); 88 } 89 90 @Test 91 @SmallTest 92 public void testFilter_carrierAppPresent_handled() throws Exception { 93 mockCarrierApp(); 94 mockCarrierAppStubResults( 95 CarrierMessagingService.RECEIVE_OPTIONS_DROP, mICarrierAppMessagingService); 96 assertTrue(mCarrierServicesSmsFilterUT.filter()); 97 98 verify(mFilterCallback, timeout(100)) 99 .onFilterComplete(eq(CarrierMessagingService.RECEIVE_OPTIONS_DROP)); 100 } 101 102 @Test 103 @SmallTest 104 public void testFilter_systemAppPresent_handled() throws Exception { 105 mockSystemApp(); 106 mockCarrierAppStubResults( 107 CarrierMessagingService.RECEIVE_OPTIONS_DROP, mISystemCarrierMessagingService); 108 109 assertTrue(mCarrierServicesSmsFilterUT.filter()); 110 111 verify(mFilterCallback, timeout(100)) 112 .onFilterComplete(eq(CarrierMessagingService.RECEIVE_OPTIONS_DROP)); 113 } 114 115 @Test 116 @SmallTest 117 public void testFilter_bothCarrierAndSystemAppPresent_carrierAppDecides() throws Exception { 118 mockCarrierApp(); 119 mockSystemApp(); 120 mockCarrierAppStubResults( 121 CarrierMessagingService.RECEIVE_OPTIONS_DEFAULT, mICarrierAppMessagingService); 122 mockCarrierAppStubResults( 123 CarrierMessagingService.RECEIVE_OPTIONS_DROP, mISystemCarrierMessagingService); 124 125 assertTrue(mCarrierServicesSmsFilterUT.filter()); 126 127 verify(mFilterCallback, timeout(100)) 128 .onFilterComplete(eq(CarrierMessagingService.RECEIVE_OPTIONS_DEFAULT)); 129 } 130 131 private void mockCarrierApp() 132 throws RemoteException { 133 mContextFixture.addService( 134 CarrierMessagingService.SERVICE_INTERFACE, 135 new ComponentName(CARRIER_APP_PACKAGE_NAME, "CarrierAppFilterClass"), 136 CARRIER_APP_PACKAGE_NAME, 137 mICarrierAppMessagingService, 138 new ServiceInfo()); 139 mockUiccWithCarrierApp(); 140 } 141 142 private void mockUiccWithCarrierApp() { 143 when(mUiccController.getUiccCard(mPhone.getPhoneId())).thenReturn(mUiccCard); 144 List<String> carrierPackages = new ArrayList<>(); 145 carrierPackages.add(CARRIER_APP_PACKAGE_NAME); 146 when(mUiccCard.getCarrierPackageNamesForIntent( 147 any(PackageManager.class), any(Intent.class))).thenReturn(carrierPackages); 148 } 149 150 private void mockSystemApp() { 151 ServiceInfo serviceInfo = new ServiceInfo(); 152 serviceInfo.packageName = SYSTEM_APP_PACKAGE_NAME; 153 mContextFixture.addService( 154 CarrierMessagingService.SERVICE_INTERFACE, 155 new ComponentName(SYSTEM_APP_PACKAGE_NAME, "SystemFilterClass"), 156 SYSTEM_APP_PACKAGE_NAME, 157 mISystemCarrierMessagingService, 158 serviceInfo); 159 } 160 161 private void mockCarrierAppStubResults(final int result, ICarrierMessagingService.Stub stub) 162 throws RemoteException { 163 when(stub.queryLocalInterface(anyString())).thenReturn(stub); 164 when(stub.asBinder()).thenReturn(stub); 165 doAnswer(new Answer<Void>() { 166 @Override 167 public Void answer(InvocationOnMock invocation) throws Throwable { 168 Object[] args = invocation.getArguments(); 169 ICarrierMessagingCallback callback = (ICarrierMessagingCallback) args[4]; 170 callback.onFilterComplete(result); 171 return null; 172 } 173 }).when(stub).filterSms( 174 any(MessagePdu.class), anyString(), anyInt(), anyInt(), 175 any(ICarrierMessagingCallback.class)); 176 } 177} 178