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.permissioncontroller.PermissionControllerProto.PermissionControllerDumpProto 24 import com.google.common.truth.Truth.assertThat 25 import com.google.protobuf.InvalidProtocolBufferException 26 import org.junit.Assert.fail 27 import org.junit.Assume.assumeTrue 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import java.nio.charset.StandardCharsets.UTF_8 31 32 @RunWith(AndroidJUnit4::class) 33 class DumpTest { 34 private val OS_PKG = "android" 35 36 private val instrumentation = InstrumentationRegistry.getInstrumentation() 37 getDumpnull38 private fun getDump(): PermissionControllerDumpProto { 39 val dumpFile = instrumentation.getUiAutomation() 40 .executeShellCommand("dumpsys permissionmgr --proto") 41 val dump = AutoCloseInputStream(dumpFile).readBytes() 42 43 try { 44 return PermissionControllerDumpProto.parseFrom(dump) 45 } catch (e: InvalidProtocolBufferException) { 46 fail("Cannot parse proto from ${String(dump, UTF_8)}") 47 throw e 48 } 49 } 50 51 @Test autoRevokeDumpHasCurrentUsernull52 fun autoRevokeDumpHasCurrentUser() { 53 val dump = getDump() 54 55 // Sometimes the dump takes to long to get generated, esp. on low end devices 56 assumeTrue(dump.autoRevoke.usersList.isNotEmpty()) 57 58 assertThat(dump.autoRevoke.usersList.map { it.userId }).contains(myUserId()) 59 } 60 61 @Test autoRevokeDumpHasAndroidPackagenull62 fun autoRevokeDumpHasAndroidPackage() { 63 val dump = getDump() 64 65 // Sometimes the dump takes to long to get generated, esp. on low end devices 66 assumeTrue(dump.autoRevoke.usersList.isNotEmpty()) 67 68 assertThat(dump.autoRevoke.usersList[myUserId()].packagesList.map { it.packageName }) 69 .contains(OS_PKG) 70 } 71 } 72