blob: f47dfdd191fb8cf97c8d140904c92b399709354d [file] [log] [blame]
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +01001/*
2 * Copyright (C) 2019 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
Song Pan75147d52019-11-19 00:57:46 +000017package android.content.integrity;
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010018
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000019import static com.google.common.truth.Truth.assertThat;
20
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000021import static org.testng.Assert.expectThrows;
Song Pan097f65d2019-11-10 18:02:52 +000022
23import android.os.Parcel;
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010024
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
Khaled Abdelmohsena4477fa2019-10-09 14:35:32 +010029import java.util.Arrays;
30import java.util.Collections;
31
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010032@RunWith(JUnit4.class)
Song Pan75147d52019-11-19 00:57:46 +000033public class CompoundFormulaTest {
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010034
Song Pan097f65d2019-11-10 18:02:52 +000035 private static final AtomicFormula ATOMIC_FORMULA_1 =
Song Pan75147d52019-11-19 00:57:46 +000036 new AtomicFormula.StringAtomicFormula(
37 AtomicFormula.PACKAGE_NAME, "test1", /* isHashedValue= */ false);
Song Pan097f65d2019-11-10 18:02:52 +000038 private static final AtomicFormula ATOMIC_FORMULA_2 =
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000039 new AtomicFormula.LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 1);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010040
41 @Test
Song Pan75147d52019-11-19 00:57:46 +000042 public void testValidCompoundFormula() {
43 CompoundFormula compoundFormula =
44 new CompoundFormula(
45 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010046
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000047 assertThat(compoundFormula.getConnector()).isEqualTo(CompoundFormula.AND);
48 assertThat(compoundFormula.getFormulas()).containsAllOf(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010049 }
50
51 @Test
52 public void testValidateAuxiliaryFormula_binaryConnectors() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000053 Exception e =
54 expectThrows(
55 IllegalArgumentException.class,
56 () ->
57 new CompoundFormula(
58 CompoundFormula.AND,
59 Collections.singletonList(ATOMIC_FORMULA_1)));
60 assertThat(e.getMessage()).matches("Connector AND must have at least 2 formulas");
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010061 }
62
63 @Test
64 public void testValidateAuxiliaryFormula_unaryConnectors() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000065 Exception e =
66 expectThrows(
67 IllegalArgumentException.class,
68 () ->
69 new CompoundFormula(
70 CompoundFormula.NOT,
71 Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)));
72 assertThat(e.getMessage()).matches("Connector NOT must have 1 formula only");
Song Pan097f65d2019-11-10 18:02:52 +000073 }
74
75 @Test
Song Pan097f65d2019-11-10 18:02:52 +000076 public void testParcelUnparcel() {
Song Pan75147d52019-11-19 00:57:46 +000077 CompoundFormula formula =
78 new CompoundFormula(
79 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_2, ATOMIC_FORMULA_1));
Song Pan097f65d2019-11-10 18:02:52 +000080 Parcel p = Parcel.obtain();
81 formula.writeToParcel(p, 0);
82 p.setDataPosition(0);
Song Pan097f65d2019-11-10 18:02:52 +000083
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000084 assertThat(CompoundFormula.CREATOR.createFromParcel(p)).isEqualTo(formula);
Song Pan097f65d2019-11-10 18:02:52 +000085 }
86
Khaled Abdelmohsen5809a5f2019-11-25 18:20:46 +000087 @Test
Song Pan75147d52019-11-19 00:57:46 +000088 public void testInvalidCompoundFormula_invalidConnector() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000089 Exception e =
90 expectThrows(
91 IllegalArgumentException.class,
92 () ->
93 new CompoundFormula(
94 /* connector= */ -1,
95 Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)));
96 assertThat(e.getMessage()).matches("Unknown connector: -1");
Khaled Abdelmohsen5809a5f2019-11-25 18:20:46 +000097 }
98
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000099 @Test
100 public void testFormulaMatches_notFalse_true() {
101 AppInstallMetadata appInstallMetadata =
102 getAppInstallMetadataBuilder().setPackageName("test2").build();
103
104 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isFalse();
105
106 CompoundFormula compoundFormula =
107 new CompoundFormula(CompoundFormula.NOT, Arrays.asList(ATOMIC_FORMULA_1));
108 assertThat(compoundFormula.matches(appInstallMetadata)).isTrue();
109 }
110
111 @Test
112 public void testFormulaMatches_notTrue_false() {
113 AppInstallMetadata appInstallMetadata =
114 getAppInstallMetadataBuilder().setPackageName("test1").build();
115
116 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
117
118 CompoundFormula compoundFormula =
119 new CompoundFormula(CompoundFormula.NOT, Arrays.asList(ATOMIC_FORMULA_1));
120 assertThat(compoundFormula.matches(appInstallMetadata)).isFalse();
121 }
122
123 @Test
124 public void testFormulaMatches_trueAndTrue_true() {
125 CompoundFormula compoundFormula =
126 new CompoundFormula(
127 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
128 AppInstallMetadata appInstallMetadata =
129 getAppInstallMetadataBuilder().setPackageName("test1").setVersionCode(1).build();
130 // validate assumptions about the metadata
131 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
132 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
133
134 assertThat(compoundFormula.matches(appInstallMetadata)).isTrue();
135 }
136
137 @Test
138 public void testFormulaMatches_trueAndFalse_false() {
139 CompoundFormula compoundFormula =
140 new CompoundFormula(
141 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
142 AppInstallMetadata appInstallMetadata =
143 getAppInstallMetadataBuilder().setPackageName("test1").setVersionCode(2).build();
144
145 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
146 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isFalse();
147 assertThat(compoundFormula.matches(appInstallMetadata)).isFalse();
148 }
149
150 @Test
151 public void testFormulaMatches_falseAndTrue_false() {
152 CompoundFormula compoundFormula =
153 new CompoundFormula(
154 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
155 AppInstallMetadata appInstallMetadata =
156 getAppInstallMetadataBuilder().setPackageName("test2").setVersionCode(1).build();
157
158 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isFalse();
159 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isTrue();
160 assertThat(compoundFormula.matches(appInstallMetadata)).isFalse();
161 }
162
163 @Test
164 public void testFormulaMatches_falseAndFalse_false() {
165 CompoundFormula compoundFormula =
166 new CompoundFormula(
167 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
168 AppInstallMetadata appInstallMetadata =
169 getAppInstallMetadataBuilder().setPackageName("test2").setVersionCode(2).build();
170
171 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isFalse();
172 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isFalse();
173 assertThat(compoundFormula.matches(appInstallMetadata)).isFalse();
174 }
175
176 @Test
177 public void testFormulaMatches_trueOrTrue_true() {
178 CompoundFormula compoundFormula =
179 new CompoundFormula(
180 CompoundFormula.OR, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
181 AppInstallMetadata appInstallMetadata =
182 getAppInstallMetadataBuilder().setPackageName("test1").setVersionCode(1).build();
183
184 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
185 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isTrue();
186 assertThat(compoundFormula.matches(appInstallMetadata)).isTrue();
187 }
188
189 @Test
190 public void testFormulaMatches_trueOrFalse_true() {
191 CompoundFormula compoundFormula =
192 new CompoundFormula(
193 CompoundFormula.OR, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
194 AppInstallMetadata appInstallMetadata =
195 getAppInstallMetadataBuilder().setPackageName("test1").setVersionCode(2).build();
196
197 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isTrue();
198 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isFalse();
199 assertThat(compoundFormula.matches(appInstallMetadata)).isTrue();
200 }
201
202 @Test
203 public void testFormulaMatches_falseOrTrue_true() {
204 CompoundFormula compoundFormula =
205 new CompoundFormula(
206 CompoundFormula.OR, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
207 AppInstallMetadata appInstallMetadata =
208 getAppInstallMetadataBuilder().setPackageName("test2").setVersionCode(1).build();
209
210 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isFalse();
211 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isTrue();
212 assertThat(compoundFormula.matches(appInstallMetadata)).isTrue();
213 }
214
215 @Test
216 public void testFormulaMatches_falseOrFalse_false() {
217 CompoundFormula compoundFormula =
218 new CompoundFormula(
219 CompoundFormula.OR, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
220 AppInstallMetadata appInstallMetadata =
221 getAppInstallMetadataBuilder().setPackageName("test2").setVersionCode(2).build();
222
223 assertThat(ATOMIC_FORMULA_1.matches(appInstallMetadata)).isFalse();
224 assertThat(ATOMIC_FORMULA_2.matches(appInstallMetadata)).isFalse();
225 assertThat(compoundFormula.matches(appInstallMetadata)).isFalse();
226 }
227
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000228 @Test
229 public void testIsAppCertificateFormula_false() {
230 CompoundFormula compoundFormula =
231 new CompoundFormula(
232 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
233
234 assertThat(compoundFormula.isAppCertificateFormula()).isFalse();
235 }
236
237 @Test
238 public void testIsAppCertificateFormula_true() {
239 AtomicFormula appCertFormula =
240 new AtomicFormula.StringAtomicFormula(AtomicFormula.APP_CERTIFICATE,
241 "app.cert", /* isHashed= */false);
242 CompoundFormula compoundFormula =
243 new CompoundFormula(
244 CompoundFormula.AND,
245 Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2, appCertFormula));
246
247 assertThat(compoundFormula.isAppCertificateFormula()).isTrue();
248 }
249
250 @Test
251 public void testIsInstallerFormula_false() {
252 CompoundFormula compoundFormula =
253 new CompoundFormula(
254 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2));
255
256 assertThat(compoundFormula.isInstallerFormula()).isFalse();
257 }
258
259 @Test
260 public void testIsInstallerFormula_installerName_true() {
261 AtomicFormula installerNameFormula =
262 new AtomicFormula.StringAtomicFormula(AtomicFormula.INSTALLER_NAME,
263 "com.test.installer", /* isHashed= */false);
264 CompoundFormula compoundFormula =
265 new CompoundFormula(
266 CompoundFormula.AND,
267 Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2, installerNameFormula));
268
269 assertThat(compoundFormula.isInstallerFormula()).isTrue();
270 }
271
272 @Test
273 public void testIsInstallerFormula_installerCertificate_true() {
274 AtomicFormula installerCertificateFormula =
275 new AtomicFormula.StringAtomicFormula(AtomicFormula.INSTALLER_CERTIFICATE,
276 "cert", /* isHashed= */false);
277 CompoundFormula compoundFormula =
278 new CompoundFormula(
279 CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2,
280 installerCertificateFormula));
281
282 assertThat(compoundFormula.isInstallerFormula()).isTrue();
283 }
284
Song Pan097f65d2019-11-10 18:02:52 +0000285 /** Returns a builder with all fields filled with some dummy data. */
286 private AppInstallMetadata.Builder getAppInstallMetadataBuilder() {
287 return new AppInstallMetadata.Builder()
288 .setPackageName("abc")
289 .setAppCertificate("abc")
290 .setInstallerCertificate("abc")
291 .setInstallerName("abc")
292 .setVersionCode(-1)
293 .setIsPreInstalled(true);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100294 }
295}