1 /* 2 * Copyright (C) 2019 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 17 package com.android.mms.service; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.testng.Assert.assertThrows; 22 23 import android.content.Intent; 24 import android.net.Uri; 25 import android.os.Process; 26 import android.os.RemoteException; 27 28 import com.android.internal.telephony.IMms; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.Robolectric; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.shadows.ShadowBinder; 36 37 @RunWith(RobolectricTestRunner.class) 38 public final class MmsServiceRoboTest { 39 private IMms.Stub binder; 40 41 @Before setUp()42 public void setUp() { 43 MmsService mmsService = Robolectric.setupService(MmsService.class); 44 45 final Intent intent = new Intent(); 46 47 binder = (IMms.Stub) mmsService.onBind(intent); 48 } 49 50 @Test testOnBind_IsNotNull()51 public void testOnBind_IsNotNull() { 52 assertThat(binder).isNotNull(); 53 } 54 55 @Test testSendMessage_DoesNotThrowIfSystemUid()56 public void testSendMessage_DoesNotThrowIfSystemUid() throws RemoteException { 57 ShadowBinder.setCallingUid(Process.SYSTEM_UID); 58 binder.sendMessage(/* subId= */ 0, "callingPkg", Uri.parse("contentUri"), 59 "locationUrl", /* configOverrides= */ null, /* sentIntent= */ null, 60 /* messageId= */ 0L, /* attributionTag= */ null); 61 } 62 63 @Test testSendMessageThrows_IfNotSystemUid()64 public void testSendMessageThrows_IfNotSystemUid() { 65 assertThrows(SecurityException.class, 66 () -> binder.sendMessage(/* subId= */ 0, "callingPkg", Uri.parse("contentUri"), 67 "locationUrl", /* configOverrides= */ null, /* sentIntent= */ null, 68 /* messageId= */ 0L, /* attributionTag= */ null)); 69 } 70 } 71