1 /*
2  * Copyright (C) 2021 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.cts.packagemanager.verify.domain.android
18 
19 import android.content.Context
20 import android.content.pm.verify.domain.DomainVerificationManager
21 import android.content.pm.verify.domain.DomainVerificationUserState
22 import com.android.compatibility.common.util.ShellUtils
23 import com.android.cts.packagemanager.verify.domain.android.DomainUtils.DECLARING_PKG_1_COMPONENT
24 import com.android.cts.packagemanager.verify.domain.android.DomainUtils.DECLARING_PKG_2_COMPONENT
25 import com.android.cts.packagemanager.verify.domain.java.DomainUtils
26 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DECLARING_PKG_NAME_1
27 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DECLARING_PKG_NAME_2
28 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DOMAIN_1
29 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DOMAIN_2
30 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DOMAIN_3
31 import com.android.cts.packagemanager.verify.domain.java.DomainUtils.DOMAIN_7
32 import com.google.common.truth.Truth
33 
34 object SharedVerifications {
35 
resetnull36     fun reset(context: Context, resetEnable: Boolean = false) {
37         DomainUtils.ALL_PACKAGES.forEach {
38             ShellUtils.runShellCommand(DomainUtils.setAppLinks(it, "STATE_NO_RESPONSE", "all"))
39             ShellUtils.runShellCommand(DomainUtils.resetAppLinks(it))
40             ShellUtils.runShellCommand(DomainUtils.setAppLinksAllowed(it, context.userId, true))
41             ShellUtils.runShellCommand(
42                 DomainUtils.setAppLinksUserSelection(it, context.userId, false, "all")
43             )
44         }
45 
46         // Ignore if these fail, since not all tests will make use of both packages
47         if (resetEnable) {
48             val enablePrefix = "pm enable --user ${context.userId}"
49             ShellUtils.runShellCommand("$enablePrefix $DECLARING_PKG_NAME_1")
50             ShellUtils.runShellCommand("$enablePrefix $DECLARING_PKG_NAME_2")
51             ShellUtils.runShellCommand(
52                 "$enablePrefix ${DECLARING_PKG_1_COMPONENT.flattenToString()}"
53             )
54             ShellUtils.runShellCommand(
55                 "$enablePrefix ${DECLARING_PKG_2_COMPONENT.flattenToString()}"
56             )
57         }
58     }
59 
verifyDomainsnull60     fun verifyDomains(context: Context) {
61         val packageName = DECLARING_PKG_NAME_1
62         val user = context.user
63         val userId = context.userId
64         val manager = context.getSystemService(DomainVerificationManager::class.java)!!
65         manager.getDomainVerificationUserState(packageName)!!.also {
66             Truth.assertThat(it.packageName).isEqualTo(packageName)
67             Truth.assertThat(it.isLinkHandlingAllowed).isTrue()
68             Truth.assertThat(it.user).isEqualTo(user)
69             Truth.assertThat(it.hostToStateMap).containsExactlyEntriesIn(
70                 mapOf(
71                     DOMAIN_1 to DomainVerificationUserState.DOMAIN_STATE_NONE,
72                     DOMAIN_2 to DomainVerificationUserState.DOMAIN_STATE_NONE,
73                     DOMAIN_3 to DomainVerificationUserState.DOMAIN_STATE_NONE,
74                     DOMAIN_7 to DomainVerificationUserState.DOMAIN_STATE_NONE,
75                 )
76             )
77         }
78 
79         // Try to approve both 1 and 2, but only 1 is marked autoVerify
80         ShellUtils.runShellCommand(
81             DomainUtils.setAppLinks(packageName, "STATE_APPROVED", DOMAIN_1, DOMAIN_2)
82         )
83 
84         manager.getDomainVerificationUserState(packageName)!!.also {
85             Truth.assertThat(it.packageName).isEqualTo(packageName)
86             Truth.assertThat(it.isLinkHandlingAllowed).isTrue()
87             Truth.assertThat(it.user).isEqualTo(user)
88             Truth.assertThat(it.hostToStateMap).containsExactlyEntriesIn(
89                 mapOf(
90                     DOMAIN_1 to DomainVerificationUserState.DOMAIN_STATE_VERIFIED,
91                     DOMAIN_2 to DomainVerificationUserState.DOMAIN_STATE_NONE,
92                     DOMAIN_3 to DomainVerificationUserState.DOMAIN_STATE_NONE,
93                     DOMAIN_7 to DomainVerificationUserState.DOMAIN_STATE_NONE,
94                 )
95             )
96         }
97 
98         ShellUtils.runShellCommand(
99             DomainUtils.setAppLinksUserSelection(packageName, userId, true, DOMAIN_1, DOMAIN_2)
100         )
101 
102         manager.getDomainVerificationUserState(packageName)!!.also {
103             Truth.assertThat(it.packageName).isEqualTo(packageName)
104             Truth.assertThat(it.isLinkHandlingAllowed).isTrue()
105             Truth.assertThat(it.user).isEqualTo(user)
106             Truth.assertThat(it.hostToStateMap).containsExactlyEntriesIn(
107                 mapOf(
108                     DOMAIN_1 to DomainVerificationUserState.DOMAIN_STATE_VERIFIED,
109                     DOMAIN_2 to DomainVerificationUserState.DOMAIN_STATE_SELECTED,
110                     DOMAIN_3 to DomainVerificationUserState.DOMAIN_STATE_NONE,
111                     DOMAIN_7 to DomainVerificationUserState.DOMAIN_STATE_NONE,
112                 )
113             )
114         }
115 
116         ShellUtils.runShellCommand(DomainUtils.setAppLinksAllowed(packageName, userId, false))
117 
118         manager.getDomainVerificationUserState(packageName)!!.also {
119             Truth.assertThat(it.packageName).isEqualTo(packageName)
120             Truth.assertThat(it.isLinkHandlingAllowed).isFalse()
121             Truth.assertThat(it.user).isEqualTo(user)
122             Truth.assertThat(it.hostToStateMap).containsExactlyEntriesIn(
123                 mapOf(
124                     DOMAIN_1 to DomainVerificationUserState.DOMAIN_STATE_VERIFIED,
125                     DOMAIN_2 to DomainVerificationUserState.DOMAIN_STATE_SELECTED,
126                     DOMAIN_3 to DomainVerificationUserState.DOMAIN_STATE_NONE,
127                     DOMAIN_7 to DomainVerificationUserState.DOMAIN_STATE_NONE,
128                 )
129             )
130         }
131     }
132 }
133