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.common.AdServicesExtendedMockitoTestCase;
26 import com.android.adservices.service.common.compat.ProcessCompatUtils;
27 import com.android.dx.mockito.inline.extended.ExtendedMockito;
28 import com.android.modules.utils.build.SdkLevel;
29 import com.android.modules.utils.testing.ExtendedMockitoRule.MockStatic;
30 
31 import org.junit.Test;
32 
33 @MockStatic(Process.class)
34 @MockStatic(ProcessCompatUtils.class)
35 public final class SdkRuntimeUtilTest extends AdServicesExtendedMockitoTestCase {
36     @Test
testCallingUidIsNotSdkSandbox_returnParameterUid()37     public void testCallingUidIsNotSdkSandbox_returnParameterUid() {
38         int uid = 400;
39         ExtendedMockito.doReturn(false).when(() -> ProcessCompatUtils.isSdkSandboxUid(uid));
40         assertThat(SdkRuntimeUtil.getCallingAppUid(uid)).isEqualTo(uid);
41     }
42 
43     @Test
testCallingUidIsSdkSandbox_returnAppUid()44     public void testCallingUidIsSdkSandbox_returnAppUid() {
45         assumeTrue(SdkLevel.isAtLeastT());
46 
47         int sdkUid = 400;
48         int appUid = 200;
49         ExtendedMockito.doReturn(true).when(() -> ProcessCompatUtils.isSdkSandboxUid(sdkUid));
50         ExtendedMockito.doReturn(appUid).when(() -> Process.getAppUidForSdkSandboxUid(sdkUid));
51         assertThat(SdkRuntimeUtil.getCallingAppUid(sdkUid)).isEqualTo(appUid);
52     }
53 }
54