1 /* 2 * Copyright (C) 2022 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.adservices.service.common; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assume.assumeTrue; 22 23 import android.os.Process; 24 25 import com.android.adservices.service.common.compat.ProcessCompatUtils; 26 import com.android.dx.mockito.inline.extended.ExtendedMockito; 27 import com.android.modules.utils.build.SdkLevel; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.mockito.MockitoSession; 33 34 public class SdkRuntimeUtilTest { 35 MockitoSession mMockitoSession; 36 37 @Before setUp()38 public void setUp() { 39 mMockitoSession = 40 ExtendedMockito.mockitoSession() 41 .mockStatic(Process.class) 42 .mockStatic(ProcessCompatUtils.class) 43 .initMocks(this) 44 .startMocking(); 45 } 46 47 @After tearDown()48 public void tearDown() { 49 mMockitoSession.finishMocking(); 50 } 51 52 @Test testCallingUidIsNotSdkSandbox_returnParameterUid()53 public void testCallingUidIsNotSdkSandbox_returnParameterUid() { 54 int uid = 400; 55 ExtendedMockito.doReturn(false).when(() -> ProcessCompatUtils.isSdkSandboxUid(uid)); 56 assertThat(SdkRuntimeUtil.getCallingAppUid(uid)).isEqualTo(uid); 57 } 58 59 @Test testCallingUidIsSdkSandbox_returnAppUid()60 public void testCallingUidIsSdkSandbox_returnAppUid() { 61 assumeTrue(SdkLevel.isAtLeastT()); 62 63 int sdkUid = 400; 64 int appUid = 200; 65 ExtendedMockito.doReturn(true).when(() -> ProcessCompatUtils.isSdkSandboxUid(sdkUid)); 66 ExtendedMockito.doReturn(appUid).when(() -> Process.getAppUidForSdkSandboxUid(sdkUid)); 67 assertThat(SdkRuntimeUtil.getCallingAppUid(sdkUid)).isEqualTo(appUid); 68 } 69 } 70