Fix PackageSetInstallerTest for multi-user

Exempt-From-Owner-Approval: Added owners.

The dumpsys output can include multiple users, and the test did not
check for a specific user when parsing the permission state.

Using the structured proto dump was considered, but permissions are
not currently part of it, and would not be sufficient to fix the test
for existing devices.

Bug: 169270298

Test: atest android.appsecurity.cts.PackageSetInstallerTest

Change-Id: I3988c833ef4716109a33e8754b055a5482bfa275
diff --git a/hostsidetests/appsecurity/OWNERS b/hostsidetests/appsecurity/OWNERS
index 6ad0c7a..cbfad62 100644
--- a/hostsidetests/appsecurity/OWNERS
+++ b/hostsidetests/appsecurity/OWNERS
@@ -26,9 +26,9 @@
 per-file ListeningPortsTest.java = cbrubaker@google.com
 per-file MajorVersionTest.java = toddke@google.com
 per-file OverlayHostTest.java = rtmitchell@google.com
-per-file PackageResolutionHostTest.java = toddke@google.com
-per-file PackageVisibilityTest.java = toddke@google.com
+per-file Package* = chiuwinson@google.com,patb@google.com,toddke@google.com
 per-file PermissionsHostTest.java = moltmann@google.com
+per-file Pkg* = chiuwinson@google.com,patb@google.com,toddke@google.com
 per-file PkgInstallSignatureVerificationTest.java = cbrubaker@google.com
 per-file PrivilegedUpdateTests.java = toddke@google.com
 per-file ReviewPermissionHelper = moltmann@google.com
diff --git a/hostsidetests/appsecurity/src/android/appsecurity/cts/PackageSetInstallerTest.kt b/hostsidetests/appsecurity/src/android/appsecurity/cts/PackageSetInstallerTest.kt
index 923810c..ba62f2f 100644
--- a/hostsidetests/appsecurity/src/android/appsecurity/cts/PackageSetInstallerTest.kt
+++ b/hostsidetests/appsecurity/src/android/appsecurity/cts/PackageSetInstallerTest.kt
@@ -30,7 +30,6 @@
 import android.cts.host.utils.DeviceJUnit4ClassRunnerWithParameters
 import android.cts.host.utils.DeviceJUnit4Parameterized
 import com.google.common.truth.Truth.assertThat
-import com.google.common.truth.Truth.assertWithMessage
 import org.junit.After
 import org.junit.Before
 import org.junit.Test
@@ -198,8 +197,7 @@
     }
 
     private fun assertPermission(granted: Boolean, permission: String) {
-        assertThat(device.executeShellCommand("dumpsys package $TARGET_PKG | grep $permission"))
-                .contains("$permission: granted=$granted")
+        assertThat(getPermissionString(permission)).contains("granted=$granted")
     }
 
     private fun grantPermission(permission: String) {
@@ -211,11 +209,7 @@
     }
 
     private fun assertGrantState(state: GrantState, permission: String) {
-        val output = device.executeShellCommand(
-                "dumpsys package $TARGET_PKG | grep \"$permission: granted\"").trim()
-
-        // Make sure only the expected output line is returned
-        assertWithMessage(output).that(output.lines().size).isEqualTo(1)
+        val output = getPermissionString(permission)
 
         when (state) {
             GrantState.TRUE -> {
@@ -238,6 +232,18 @@
         }
     }
 
+    private fun getPermissionString(permission: String) =
+            device.executeShellCommand("dumpsys package $TARGET_PKG")
+                    .lineSequence()
+                    .dropWhile { !it.startsWith("Packages:") } // Wait for package header
+                    .drop(1) // Drop the package header itself
+                    .takeWhile { it.isEmpty() || it.first().isWhitespace() } // Until next header
+                    .dropWhile { !it.trim().startsWith("User $mPrimaryUserId:") } // Find user
+                    .drop(1) // Drop the user header itself
+                    .takeWhile { !it.trim().startsWith("User") } // Until next user
+                    .filter { it.contains("$permission: granted=") }
+                    .single()
+
     enum class GrantState {
         // Granted in full, unrestricted
         TRUE,