1/*
2 * Copyright (C) 2011 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
17package com.android.server.pm;
18
19import android.content.pm.PackageManager;
20import com.android.server.pm.PackageVerificationState;
21
22import android.test.AndroidTestCase;
23
24public class PackageVerificationStateTest extends AndroidTestCase {
25    private static final int REQUIRED_UID = 1948;
26
27    private static final int SUFFICIENT_UID_1 = 1005;
28
29    private static final int SUFFICIENT_UID_2 = 8938;
30
31    public void testPackageVerificationState_OnlyRequiredVerifier_AllowedInstall() {
32        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
33
34        assertFalse("Verification should not be marked as complete yet",
35                state.isVerificationComplete());
36
37        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_ALLOW);
38
39        assertTrue("Verification should be considered complete now",
40                state.isVerificationComplete());
41
42        assertTrue("Installation should be marked as allowed",
43                state.isInstallAllowed());
44    }
45
46    public void testPackageVerificationState_OnlyRequiredVerifier_DeniedInstall() {
47        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
48
49        assertFalse("Verification should not be marked as complete yet",
50                state.isVerificationComplete());
51
52        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_REJECT);
53
54        assertTrue("Verification should be considered complete now",
55                state.isVerificationComplete());
56
57        assertFalse("Installation should be marked as allowed",
58                state.isInstallAllowed());
59    }
60
61    public void testPackageVerificationState_RequiredAndOneSufficient_RequiredDeniedInstall() {
62        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
63
64        assertFalse("Verification should not be marked as complete yet",
65                state.isVerificationComplete());
66
67        state.addSufficientVerifier(SUFFICIENT_UID_1);
68
69        assertFalse("Verification should not be marked as complete yet",
70                state.isVerificationComplete());
71
72        state.setVerifierResponse(SUFFICIENT_UID_1, PackageManager.VERIFICATION_ALLOW);
73
74        assertFalse("Verification should not be marked as complete yet",
75                state.isVerificationComplete());
76
77        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_REJECT);
78
79        assertTrue("Verification should be considered complete now",
80                state.isVerificationComplete());
81
82        assertFalse("Installation should be marked as allowed",
83                state.isInstallAllowed());
84    }
85
86    public void testPackageVerificationState_RequiredAndOneSufficient_SufficientDeniedInstall() {
87        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
88
89        assertFalse("Verification should not be marked as complete yet",
90                state.isVerificationComplete());
91
92        state.addSufficientVerifier(SUFFICIENT_UID_1);
93
94        assertFalse("Verification should not be marked as complete yet",
95                state.isVerificationComplete());
96
97        state.setVerifierResponse(SUFFICIENT_UID_1, PackageManager.VERIFICATION_REJECT);
98
99        assertFalse("Verification should not be marked as complete yet",
100                state.isVerificationComplete());
101
102        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_ALLOW);
103
104        assertTrue("Verification should be considered complete now",
105                state.isVerificationComplete());
106
107        assertFalse("Installation should be marked as allowed",
108                state.isInstallAllowed());
109    }
110
111    public void testPackageVerificationState_RequiredAndTwoSufficient_OneSufficientIsEnough() {
112        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
113
114        assertFalse("Verification should not be marked as complete yet",
115                state.isVerificationComplete());
116
117        state.addSufficientVerifier(SUFFICIENT_UID_1);
118        state.addSufficientVerifier(SUFFICIENT_UID_2);
119
120        assertFalse("Verification should not be marked as complete yet",
121                state.isVerificationComplete());
122
123        state.setVerifierResponse(SUFFICIENT_UID_1, PackageManager.VERIFICATION_ALLOW);
124
125        assertFalse("Verification should not be marked as complete yet",
126                state.isVerificationComplete());
127
128        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_ALLOW);
129
130        assertTrue("Verification should be considered complete now",
131                state.isVerificationComplete());
132
133        assertTrue("Installation should be marked as allowed",
134                state.isInstallAllowed());
135    }
136
137    public void testPackageVerificationState_RequiredAndTwoSufficient_SecondSufficientIsEnough() {
138        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
139
140        assertFalse("Verification should not be marked as complete yet",
141                state.isVerificationComplete());
142
143        state.addSufficientVerifier(SUFFICIENT_UID_1);
144        state.addSufficientVerifier(SUFFICIENT_UID_2);
145
146        assertFalse("Verification should not be marked as complete yet",
147                state.isVerificationComplete());
148
149        state.setVerifierResponse(REQUIRED_UID, PackageManager.VERIFICATION_ALLOW);
150
151        assertFalse("Verification should not be marked as complete yet",
152                state.isVerificationComplete());
153
154        state.setVerifierResponse(SUFFICIENT_UID_1, PackageManager.VERIFICATION_REJECT);
155
156        assertFalse("Verification should not be marked as complete yet",
157                state.isVerificationComplete());
158
159        state.setVerifierResponse(SUFFICIENT_UID_2, PackageManager.VERIFICATION_ALLOW);
160
161        assertTrue("Verification should be considered complete now",
162                state.isVerificationComplete());
163
164        assertTrue("Installation should be marked as allowed",
165                state.isInstallAllowed());
166    }
167
168    public void testPackageVerificationState_RequiredAndTwoSufficient_RequiredOverrides() {
169        PackageVerificationState state = new PackageVerificationState(REQUIRED_UID, null);
170
171        assertFalse("Verification should not be marked as complete yet",
172                state.isVerificationComplete());
173
174        state.addSufficientVerifier(SUFFICIENT_UID_1);
175        state.addSufficientVerifier(SUFFICIENT_UID_2);
176
177        assertFalse("Verification should not be marked as complete yet",
178                state.isVerificationComplete());
179
180        state.setVerifierResponse(REQUIRED_UID,
181                PackageManager.VERIFICATION_ALLOW_WITHOUT_SUFFICIENT);
182
183        assertTrue("Verification should be marked as complete immediately",
184                state.isVerificationComplete());
185
186        assertTrue("Installation should be marked as allowed",
187                state.isInstallAllowed());
188
189        state.setVerifierResponse(SUFFICIENT_UID_1, PackageManager.VERIFICATION_REJECT);
190
191        assertTrue("Verification should still be marked as completed",
192                state.isVerificationComplete());
193
194        assertTrue("Installation should be marked as allowed still",
195                state.isInstallAllowed());
196
197        state.setVerifierResponse(SUFFICIENT_UID_2, PackageManager.VERIFICATION_ALLOW);
198
199        assertTrue("Verification should still be complete",
200                state.isVerificationComplete());
201
202        assertTrue("Installation should be marked as allowed still",
203                state.isInstallAllowed());
204    }
205}
206