1 /* 2 * Copyright (C) 2020 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.permissioncontroller.tests.outofprocess 18 19 import android.os.ParcelFileDescriptor.AutoCloseInputStream 20 import android.os.UserHandle.myUserId 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import androidx.test.platform.app.InstrumentationRegistry 23 import com.android.modules.utils.build.SdkLevel 24 import com.android.permissioncontroller.PermissionControllerProto.PermissionControllerDumpProto 25 import com.google.common.truth.Truth.assertThat 26 import com.google.protobuf.InvalidProtocolBufferException 27 import java.nio.charset.StandardCharsets.UTF_8 28 import org.junit.Assert.fail 29 import org.junit.Assume.assumeFalse 30 import org.junit.Assume.assumeTrue 31 import org.junit.Before 32 import org.junit.Test 33 import org.junit.runner.RunWith 34 35 @RunWith(AndroidJUnit4::class) 36 class DumpTest { 37 private val OS_PKG = "android" 38 39 private val instrumentation = InstrumentationRegistry.getInstrumentation() 40 getDumpnull41 private fun getDump(): PermissionControllerDumpProto { 42 val dumpFile = 43 instrumentation.getUiAutomation().executeShellCommand("dumpsys permissionmgr --proto") 44 val dump = AutoCloseInputStream(dumpFile).readBytes() 45 46 try { 47 return PermissionControllerDumpProto.parseFrom(dump) 48 } catch (e: InvalidProtocolBufferException) { 49 fail("Cannot parse proto from ${String(dump, UTF_8)}") 50 throw e 51 } 52 } 53 54 @Before setUpnull55 fun setUp() { 56 // We no longer dump auto revoke data since T. 57 assumeFalse(SdkLevel.isAtLeastT()) 58 } 59 60 @Test autoRevokeDumpHasCurrentUsernull61 fun autoRevokeDumpHasCurrentUser() { 62 val dump = getDump() 63 64 // Sometimes the dump takes to long to get generated, esp. on low end devices 65 assumeTrue(dump.autoRevoke.usersList.isNotEmpty()) 66 67 assertThat(dump.autoRevoke.usersList.map { it.userId }).contains(myUserId()) 68 } 69 70 @Test autoRevokeDumpHasAndroidPackagenull71 fun autoRevokeDumpHasAndroidPackage() { 72 val dump = getDump() 73 74 // Sometimes the dump takes to long to get generated, esp. on low end devices 75 assumeTrue(dump.autoRevoke.usersList.isNotEmpty()) 76 77 assertThat(dump.autoRevoke.usersList[myUserId()].packagesList.map { it.packageName }) 78 .contains(OS_PKG) 79 } 80 } 81