Add test for startProxyOp

Test: atest AppOpEventCollectionTest
Bug: 170336396
Change-Id: I6a9a90d2d85738f3b1114458db42de2919054120
diff --git a/tests/tests/appop/src/android/app/appops/cts/AppOpEventCollectionTest.kt b/tests/tests/appop/src/android/app/appops/cts/AppOpEventCollectionTest.kt
index efb863b..8740478 100644
--- a/tests/tests/appop/src/android/app/appops/cts/AppOpEventCollectionTest.kt
+++ b/tests/tests/appop/src/android/app/appops/cts/AppOpEventCollectionTest.kt
@@ -19,6 +19,7 @@
 import android.app.AppOpsManager
 import android.app.AppOpsManager.MAX_PRIORITY_UID_STATE
 import android.app.AppOpsManager.MIN_PRIORITY_UID_STATE
+import android.app.AppOpsManager.MODE_ALLOWED
 import android.app.AppOpsManager.OPSTR_WIFI_SCAN
 import android.app.AppOpsManager.OP_FLAGS_ALL
 import android.app.AppOpsManager.OP_FLAG_SELF
@@ -50,6 +51,23 @@
 
     private val myUid = android.os.Process.myUid()
     private val myPackage = context.packageName
+    private val otherPkg: String
+    private val otherUid: Int
+    private val firstTag = "firstProxyAttribution"
+    private val secondTag = "secondProxyAttribution"
+
+    init {
+    // Find another app to blame
+    val otherAppInfo = context.packageManager
+        .resolveActivity(Intent(ACTION_INSTALL_PACKAGE).addCategory(Intent.CATEGORY_DEFAULT)
+            .setDataAndType(Uri.parse("content://com.example/foo.apk"),
+                "application/vnd.android.package-archive"), 0)
+        ?.activityInfo?.applicationInfo
+
+        assumeNotNull(otherAppInfo)
+        otherPkg = otherAppInfo!!.packageName
+        otherUid = otherAppInfo.uid
+    }
 
     // Start an activity to make sure this app counts as being in the foreground
     @Rule
@@ -87,8 +105,8 @@
         sleep(1)
 
         assertThat(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!
-                .getLastAccessTime(MAX_PRIORITY_UID_STATE, UID_STATE_TOP, OP_FLAGS_ALL))
-                .isIn(before..beforeUidChange)
+            .getLastAccessTime(MAX_PRIORITY_UID_STATE, UID_STATE_TOP, OP_FLAGS_ALL))
+            .isIn(before..beforeUidChange)
 
         try {
             activityRule.activity.finish()
@@ -97,7 +115,7 @@
             eventually {
                 // The system remembers the time before and after the uid change as separate events
                 assertThat(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!
-                        .getLastAccessTime(UID_STATE_TOP + 1, MIN_PRIORITY_UID_STATE,
+                    .getLastAccessTime(UID_STATE_TOP + 1, MIN_PRIORITY_UID_STATE,
                         OP_FLAGS_ALL)).isAtLeast(beforeUidChange)
             }
         } finally {
@@ -220,7 +238,7 @@
         val attributionOpEntry = opEntry.attributedOpEntries[null]!!
 
         assertThat(attributionOpEntry.getLastAccessTime(OP_FLAG_TRUSTED_PROXY))
-                .isIn(before..afterTrusted)
+            .isIn(before..afterTrusted)
         assertThat(attributionOpEntry.getLastAccessTime(OP_FLAG_SELF)).isIn(afterTrusted..after)
         assertThat(opEntry.getLastAccessTime(OP_FLAG_TRUSTED_PROXY)).isIn(before..afterTrusted)
         assertThat(opEntry.getLastAccessTime(OP_FLAG_SELF)).isIn(afterTrusted..after)
@@ -233,19 +251,19 @@
     @Test
     fun noteForTwoAttributionsCheckOpEntries() {
         val before = System.currentTimeMillis()
-        appOpsManager.noteOp(OPSTR_WIFI_SCAN, myUid, myPackage, "firstAttribution", null)
+        appOpsManager.noteOp(OPSTR_WIFI_SCAN, myUid, myPackage, firstTag, null)
         val afterFirst = System.currentTimeMillis()
 
         // Make sure timestamps are distinct
         sleep(1)
 
         // self note
-        appOpsManager.noteOp(OPSTR_WIFI_SCAN, myUid, myPackage, "secondAttribution", null)
+        appOpsManager.noteOp(OPSTR_WIFI_SCAN, myUid, myPackage, secondTag, null)
         val after = System.currentTimeMillis()
 
         val opEntry = getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!
-        val firstAttributionOpEntry = opEntry.attributedOpEntries["firstAttribution"]!!
-        val secondAttributionOpEntry = opEntry.attributedOpEntries["secondAttribution"]!!
+        val firstAttributionOpEntry = opEntry.attributedOpEntries[firstTag]!!
+        val secondAttributionOpEntry = opEntry.attributedOpEntries[secondTag]!!
 
         assertThat(firstAttributionOpEntry.getLastAccessTime(OP_FLAG_SELF)).isIn(before..afterFirst)
         assertThat(secondAttributionOpEntry.getLastAccessTime(OP_FLAG_SELF)).isIn(afterFirst..after)
@@ -257,60 +275,187 @@
     @AppModeFull(reason = "instant apps cannot see other packages")
     @Test
     fun noteFromTwoProxiesAndVerifyProxyInfo() {
-        // Find another app to blame
-        val otherAppInfo = context.packageManager
-                .resolveActivity(Intent(ACTION_INSTALL_PACKAGE).addCategory(Intent.CATEGORY_DEFAULT)
-                        .setDataAndType(Uri.parse("content://com.example/foo.apk"),
-                                "application/vnd.android.package-archive"), 0)
-                ?.activityInfo?.applicationInfo
-
-        assumeNotNull(otherAppInfo)
-
-        val otherPkg = otherAppInfo!!.packageName
-        val otherUid = otherAppInfo.uid
-
         // Using the shell identity causes a trusted proxy note
         runWithShellPermissionIdentity {
-            context.createAttributionContext("firstProxyAttribution")
-                    .getSystemService(AppOpsManager::class.java)
-                    .noteProxyOp(OPSTR_WIFI_SCAN, otherPkg, otherUid, null, null)
+            context.createAttributionContext(firstTag)
+                .getSystemService(AppOpsManager::class.java)
+                .noteProxyOp(OPSTR_WIFI_SCAN, otherPkg, otherUid, null, null)
         }
 
         // Make sure timestamps are distinct
         sleep(1)
 
         // untrusted proxy note
-        context.createAttributionContext("secondProxyAttribution")
-                .getSystemService(AppOpsManager::class.java)
-                .noteProxyOp(OPSTR_WIFI_SCAN, otherPkg, otherUid, null, null)
+        context.createAttributionContext(secondTag)
+            .getSystemService(AppOpsManager::class.java)
+            .noteProxyOp(OPSTR_WIFI_SCAN, otherPkg, otherUid, null, null)
 
         val opEntry = getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!
         val attributionOpEntry = opEntry.attributedOpEntries[null]!!
 
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.packageName)
-                .isEqualTo(myPackage)
+            .isEqualTo(myPackage)
         assertThat(opEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.packageName)
-                .isEqualTo(myPackage)
+            .isEqualTo(myPackage)
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.uid)
-                .isEqualTo(myUid)
+            .isEqualTo(myUid)
         assertThat(opEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.uid).isEqualTo(myUid)
 
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.packageName)
-                .isEqualTo(myPackage)
+            .isEqualTo(myPackage)
         assertThat(opEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.packageName)
-                .isEqualTo(myPackage)
+            .isEqualTo(myPackage)
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.uid)
-                .isEqualTo(myUid)
+            .isEqualTo(myUid)
         assertThat(opEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.uid).isEqualTo(myUid)
 
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.attributionTag)
-                .isEqualTo("firstProxyAttribution")
+            .isEqualTo(firstTag)
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.attributionTag)
-                .isEqualTo("secondProxyAttribution")
+            .isEqualTo(secondTag)
 
         // If asked for all op-flags the second attribution overrides the first
         assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAGS_ALL)?.attributionTag)
-                .isEqualTo("secondProxyAttribution")
+            .isEqualTo(secondTag)
+    }
+
+    @AppModeFull(reason = "instant apps cannot see other packages")
+    @Test
+    fun startStopTrustedProxyVerifyRunningAndTime() {
+        val beforeTrusted = System.currentTimeMillis()
+        // Make sure timestamps are distinct
+        sleep(1)
+
+        lateinit var firstAttrManager: AppOpsManager
+        // Using the shell identity causes a trusted proxy op
+        runWithShellPermissionIdentity {
+            firstAttrManager = context.createAttributionContext(firstTag)!!
+                .getSystemService(AppOpsManager::class.java)!!
+            val start = firstAttrManager.startProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null,
+                null)
+            assertThat(start).isEqualTo(MODE_ALLOWED)
+            sleep(1)
+        }
+
+        with(getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!) {
+            assertThat(attributedOpEntries[null]!!.isRunning).isTrue()
+            assertThat(attributedOpEntries[null]?.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)!!
+                .packageName).isEqualTo(myPackage)
+            assertThat(attributedOpEntries[null]?.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)!!
+                .attributionTag).isEqualTo(firstTag)
+            assertThat(isRunning).isTrue()
+        }
+
+        with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
+            assertThat(attributedOpEntries[firstTag]!!.isRunning).isTrue()
+            assertThat(attributedOpEntries[firstTag]!!
+                .getLastProxyInfo(OP_FLAGS_ALL)).isNull()
+        }
+
+        firstAttrManager.finishProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null)
+        sleep(1)
+        val afterTrusted = System.currentTimeMillis()
+
+        val opEntry = getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!
+        val attributionOpEntry = opEntry.attributedOpEntries[null]!!
+        assertThat(attributionOpEntry.isRunning).isFalse()
+        assertThat(opEntry.isRunning).isFalse()
+        assertThat(attributionOpEntry.getLastAccessTime(OP_FLAG_TRUSTED_PROXIED))
+            .isIn(beforeTrusted..afterTrusted)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.packageName)
+            .isEqualTo(myPackage)
+        assertThat(opEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.packageName)
+            .isEqualTo(myPackage)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.uid)
+            .isEqualTo(myUid)
+        assertThat(opEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.uid).isEqualTo(myUid)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.attributionTag)
+            .isEqualTo(firstTag)
+    }
+
+    @AppModeFull(reason = "instant apps cannot see other packages")
+    @Test
+    fun startStopUntrustedProxyVerifyRunningAndTime() {
+        val beforeUntrusted = System.currentTimeMillis()
+        // Make sure timestamps are distinct
+        sleep(1)
+
+        // Untrusted proxy op
+        val secondAttrManager = context.createAttributionContext(secondTag)!!
+            .getSystemService(AppOpsManager::class.java)!!
+        secondAttrManager.startProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null, null)
+        with(getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!) {
+            assertThat(attributedOpEntries[null]?.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)!!
+                .packageName).isEqualTo(myPackage)
+            assertThat(attributedOpEntries[null]?.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)!!
+                .attributionTag).isEqualTo(secondTag)
+        }
+
+        with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
+            assertThat(attributedOpEntries[secondTag]!!.isRunning).isTrue()
+            assertThat(attributedOpEntries[secondTag]!!
+                .getLastProxyInfo(OP_FLAGS_ALL)).isNull()
+        }
+
+        secondAttrManager.finishProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null)
+        sleep(1)
+        val afterUntrusted = System.currentTimeMillis()
+
+        val opEntry = getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!
+        val attributionOpEntry = opEntry.attributedOpEntries[null]!!
+
+        assertThat(attributionOpEntry.isRunning).isFalse()
+        assertThat(opEntry.isRunning).isFalse()
+        assertThat(attributionOpEntry.getLastAccessTime(OP_FLAG_UNTRUSTED_PROXIED))
+            .isIn(beforeUntrusted..afterUntrusted)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.packageName)
+            .isEqualTo(myPackage)
+        assertThat(opEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.packageName)
+            .isEqualTo(myPackage)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.uid)
+            .isEqualTo(myUid)
+        assertThat(opEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.uid).isEqualTo(myUid)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.attributionTag)
+            .isEqualTo(secondTag)
+    }
+
+    @AppModeFull(reason = "instant apps cannot see other packages")
+    @Test
+    fun startStopTrustedAndUntrustedProxyVerifyProxyInfo() {
+        lateinit var firstAttrManager: AppOpsManager
+        // Using the shell identity causes a trusted proxy op
+        runWithShellPermissionIdentity {
+            firstAttrManager = context.createAttributionContext(firstTag)!!
+                .getSystemService(AppOpsManager::class.java)!!
+            val start = firstAttrManager.startProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null,
+                null)
+            sleep(1)
+        }
+
+        firstAttrManager.finishProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null)
+        sleep(1)
+
+        // Untrusted proxy op
+        val secondAttrManager = context.createAttributionContext(secondTag)!!
+            .getSystemService(AppOpsManager::class.java)!!
+        secondAttrManager.startProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null, null)
+
+        sleep(1)
+        secondAttrManager.finishProxyOp(OPSTR_WIFI_SCAN, otherUid, otherPkg, null)
+
+        val opEntry = getOpEntry(otherUid, otherPkg, OPSTR_WIFI_SCAN)!!
+        val attributionOpEntry = opEntry.attributedOpEntries[null]!!
+        assertThat(attributionOpEntry.isRunning).isFalse()
+        assertThat(opEntry.isRunning).isFalse()
+
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_TRUSTED_PROXIED)?.attributionTag)
+            .isEqualTo(firstTag)
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAG_UNTRUSTED_PROXIED)?.attributionTag)
+            .isEqualTo(secondTag)
+
+        // If asked for all op-flags the second attribution overrides the first
+        assertThat(attributionOpEntry.getLastProxyInfo(OP_FLAGS_ALL)?.attributionTag)
+            .isEqualTo(secondTag)
     }
 
     @Test
@@ -372,12 +517,12 @@
 
         with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
             assertThat(attributedOpEntries[null]!!.getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeNullAttributionStart..afterNullAttributionStart)
+                .isIn(beforeNullAttributionStart..afterNullAttributionStart)
             attributedOpEntries[TEST_ATTRIBUTION_TAG]?.let {
                 assertThat(it.getLastAccessTime(OP_FLAGS_ALL)).isAtMost(beforeNullAttributionStart)
             }
             assertThat(getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeNullAttributionStart..afterNullAttributionStart)
+                .isIn(beforeNullAttributionStart..afterNullAttributionStart)
         }
 
         val beforeFirstAttributionStart = System.currentTimeMillis()
@@ -386,11 +531,11 @@
 
         with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
             assertThat(attributedOpEntries[null]!!.getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeNullAttributionStart..afterNullAttributionStart)
+                .isIn(beforeNullAttributionStart..afterNullAttributionStart)
             assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!.getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
+                .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
             assertThat(getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
+                .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
         }
 
         appOpsManager.startOp(OPSTR_WIFI_SCAN, myUid, myPackage, TEST_ATTRIBUTION_TAG, null)
@@ -398,11 +543,11 @@
         // Nested startOps do _not_ count as another access
         with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
             assertThat(attributedOpEntries[null]!!.getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeNullAttributionStart..afterNullAttributionStart)
+                .isIn(beforeNullAttributionStart..afterNullAttributionStart)
             assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!.getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
+                .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
             assertThat(getLastAccessTime(OP_FLAGS_ALL))
-                    .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
+                .isIn(beforeFirstAttributionStart..afterFirstAttributionStart)
         }
 
         appOpsManager.finishOp(OPSTR_WIFI_SCAN, myUid, myPackage, TEST_ATTRIBUTION_TAG)
@@ -412,9 +557,9 @@
 
     @Test
     fun startStopMultipleOpsAndVerifyDuration() {
-        val beforeNullAttributionStart = SystemClock.elapsedRealtime()
+        val beforeNullAttrStart = SystemClock.elapsedRealtime()
         appOpsManager.startOp(OPSTR_WIFI_SCAN, myUid, myPackage, null, null)
-        val afterNullAttributionStart = SystemClock.elapsedRealtime()
+        val afterNullAttrStart = SystemClock.elapsedRealtime()
 
         run {
             val beforeGetOp = SystemClock.elapsedRealtime()
@@ -422,17 +567,15 @@
                 val afterGetOp = SystemClock.elapsedRealtime()
 
                 assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
                 assertThat(getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
             }
         }
 
-        val beforeAttributionStart = SystemClock.elapsedRealtime()
+        val beforeAttrStart = SystemClock.elapsedRealtime()
         appOpsManager.startOp(OPSTR_WIFI_SCAN, myUid, myPackage, TEST_ATTRIBUTION_TAG, null)
-        val afterAttributionStart = SystemClock.elapsedRealtime()
+        val afterAttrStart = SystemClock.elapsedRealtime()
 
         run {
             val beforeGetOp = SystemClock.elapsedRealtime()
@@ -440,15 +583,14 @@
                 val afterGetOp = SystemClock.elapsedRealtime()
 
                 assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
                 assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!
-                        .getLastDuration(OP_FLAGS_ALL)).isIn(beforeGetOp -
-                                afterAttributionStart..afterGetOp - beforeAttributionStart)
+                    .getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
 
                 // The last duration is the duration of the last started attribution
-                assertThat(getLastDuration(OP_FLAGS_ALL)).isIn(beforeGetOp -
-                        afterAttributionStart..afterGetOp - beforeAttributionStart)
+                assertThat(getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
             }
         }
 
@@ -462,14 +604,12 @@
                 val afterGetOp = SystemClock.elapsedRealtime()
 
                 assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
                 assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!
-                        .getLastDuration(OP_FLAGS_ALL)).isIn(beforeGetOp -
-                        afterAttributionStart..afterGetOp - beforeAttributionStart)
+                    .getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
                 assertThat(getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp -
-                                afterAttributionStart..afterGetOp - beforeAttributionStart)
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
             }
         }
 
@@ -481,19 +621,19 @@
                 val afterGetOp = SystemClock.elapsedRealtime()
 
                 assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
                 assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!
-                        .getLastDuration(OP_FLAGS_ALL)).isIn(beforeGetOp -
-                                afterAttributionStart..afterGetOp - beforeAttributionStart)
-                assertThat(getLastDuration(OP_FLAGS_ALL)).isIn(beforeGetOp -
-                                afterAttributionStart..afterGetOp - beforeAttributionStart)
+                    .getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
+                assertThat(getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeGetOp - afterAttrStart..afterGetOp - beforeAttrStart)
             }
         }
 
-        val beforeAttributionStop = SystemClock.elapsedRealtime()
+        val beforeAttrStop = SystemClock.elapsedRealtime()
         appOpsManager.finishOp(OPSTR_WIFI_SCAN, myUid, myPackage, TEST_ATTRIBUTION_TAG)
-        val afterAttributionStop = SystemClock.elapsedRealtime()
+        sleep(1)
+        val afterAttrStop = SystemClock.elapsedRealtime()
 
         run {
             val beforeGetOp = SystemClock.elapsedRealtime()
@@ -501,32 +641,27 @@
                 val afterGetOp = SystemClock.elapsedRealtime()
 
                 assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeGetOp - afterNullAttributionStart
-                                ..afterGetOp - beforeNullAttributionStart)
+                    .isIn(beforeGetOp - afterNullAttrStart..afterGetOp - beforeNullAttrStart)
                 assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!
-                        .getLastDuration(OP_FLAGS_ALL)).isIn(
-                        beforeAttributionStop - afterAttributionStart
-                                ..afterAttributionStop - beforeAttributionStart)
+                    .getLastDuration(OP_FLAGS_ALL))
+                    .isIn(beforeAttrStop - afterAttrStart..afterAttrStop - beforeAttrStart)
                 assertThat(getLastDuration(OP_FLAGS_ALL))
-                        .isIn(beforeAttributionStop - afterAttributionStart
-                                ..afterAttributionStop - beforeAttributionStart)
+                    .isIn(beforeAttrStop - afterAttrStart..afterAttrStop - beforeAttrStart)
             }
         }
 
-        val beforeNullAttributionStop = SystemClock.elapsedRealtime()
+        val beforeNullAttrStop = SystemClock.elapsedRealtime()
         appOpsManager.finishOp(OPSTR_WIFI_SCAN, myUid, myPackage, null)
-        val afterNullAttributionStop = SystemClock.elapsedRealtime()
+        val afterNullAttrStop = SystemClock.elapsedRealtime()
 
         with(getOpEntry(myUid, myPackage, OPSTR_WIFI_SCAN)!!) {
             assertThat(attributedOpEntries[null]!!.getLastDuration(OP_FLAGS_ALL))
-                    .isIn(beforeNullAttributionStop - afterNullAttributionStart
-                            ..afterNullAttributionStop - beforeNullAttributionStart)
+                .isIn(beforeNullAttrStop -
+                    afterNullAttrStart..afterNullAttrStop - beforeNullAttrStart)
             assertThat(attributedOpEntries[TEST_ATTRIBUTION_TAG]!!.getLastDuration(OP_FLAGS_ALL))
-                    .isIn(beforeAttributionStop - afterAttributionStart
-                            ..afterAttributionStop - beforeAttributionStart)
+                .isIn(beforeAttrStop - afterAttrStart..afterAttrStop - beforeAttrStart)
             assertThat(getLastDuration(OP_FLAGS_ALL))
-                    .isIn(beforeAttributionStop - afterAttributionStart
-                            ..afterAttributionStop - beforeAttributionStart)
+                .isIn(beforeAttrStop - afterAttrStart..afterAttrStop - beforeAttrStart)
         }
     }
 }