1 /* 2 * Copyright (C) 2018 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.server; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.os.Process; 22 import android.platform.test.annotations.Presubmit; 23 24 import androidx.test.InstrumentationRegistry; 25 import androidx.test.filters.SmallTest; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import com.android.server.BinderCallsStatsService.AuthorizedWorkSourceProvider; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 @Presubmit 36 public class BinderCallsStatsServiceTest { 37 @Test weTrustOurselves()38 public void weTrustOurselves() { 39 AuthorizedWorkSourceProvider workSourceProvider = new AuthorizedWorkSourceProvider() { 40 protected int getCallingUid() { 41 return Process.myUid(); 42 } 43 }; 44 workSourceProvider.systemReady(InstrumentationRegistry.getContext()); 45 46 assertEquals(1, workSourceProvider.resolveWorkSourceUid(1)); 47 } 48 49 @Test workSourceSetIfCallerHasPermission()50 public void workSourceSetIfCallerHasPermission() { 51 AuthorizedWorkSourceProvider workSourceProvider = new AuthorizedWorkSourceProvider() { 52 protected int getCallingUid() { 53 // System process uid which as UPDATE_DEVICE_STATS. 54 return 1001; 55 } 56 }; 57 workSourceProvider.systemReady(InstrumentationRegistry.getContext()); 58 59 assertEquals(1, workSourceProvider.resolveWorkSourceUid(1)); 60 } 61 62 @Test workSourceResolvedToCallingUid()63 public void workSourceResolvedToCallingUid() { 64 AuthorizedWorkSourceProvider workSourceProvider = new AuthorizedWorkSourceProvider() { 65 protected int getCallingUid() { 66 // UID without permissions. 67 return Integer.MAX_VALUE; 68 } 69 }; 70 workSourceProvider.systemReady(InstrumentationRegistry.getContext()); 71 72 assertEquals(Integer.MAX_VALUE, workSourceProvider.resolveWorkSourceUid(1)); 73 } 74 } 75