blob: bf782034c8f1cb298fd4cb2294e60e46ee9264a8 [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;
Song Pan097f65d2019-11-10 18:02:52 +000020
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +000021import static org.testng.Assert.expectThrows;
22
Song Pan75147d52019-11-19 00:57:46 +000023import android.content.integrity.AtomicFormula.BooleanAtomicFormula;
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +000024import android.content.integrity.AtomicFormula.LongAtomicFormula;
Song Pan75147d52019-11-19 00:57:46 +000025import android.content.integrity.AtomicFormula.StringAtomicFormula;
Song Pan097f65d2019-11-10 18:02:52 +000026import android.os.Parcel;
27
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010028import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32@RunWith(JUnit4.class)
33public class AtomicFormulaTest {
34
35 @Test
36 public void testValidAtomicFormula_stringValue() {
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000037 String packageName = "com.test.app";
Song Pan097f65d2019-11-10 18:02:52 +000038 StringAtomicFormula stringAtomicFormula =
Song Pan75147d52019-11-19 00:57:46 +000039 new StringAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000040 AtomicFormula.PACKAGE_NAME, packageName, /* isHashedValue= */false);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010041
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000042 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.PACKAGE_NAME);
43 assertThat(stringAtomicFormula.getValue()).isEqualTo(packageName);
44 assertThat(stringAtomicFormula.getIsHashedValue()).isFalse();
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010045 }
46
47 @Test
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000048 public void testValidAtomicFormula_stringValue_autoHash_packageNameLessThanLimit() {
49 String packageName = "com.test.app";
50 StringAtomicFormula stringAtomicFormula =
51 new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, packageName);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +010052
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +000053 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.PACKAGE_NAME);
54 assertThat(stringAtomicFormula.getValue()).isEqualTo(packageName);
55 assertThat(stringAtomicFormula.getIsHashedValue()).isFalse();
56 }
57
58 @Test
59 public void testValidAtomicFormula_stringValue_autoHash_longPackageName() {
60 String packageName = "com.test.app.test.app.test.app.test.app.test.app";
61 StringAtomicFormula stringAtomicFormula =
62 new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, packageName);
63
64 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.PACKAGE_NAME);
65 assertThat(stringAtomicFormula.getValue()).doesNotMatch(packageName);
66 assertThat(stringAtomicFormula.getIsHashedValue()).isTrue();
67 }
68
69 @Test
70 public void testValidAtomicFormula_stringValue_autoHash_installerNameLessThanLimit() {
71 String installerName = "com.test.app";
72 StringAtomicFormula stringAtomicFormula =
73 new StringAtomicFormula(AtomicFormula.INSTALLER_NAME, installerName);
74
75
76 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.INSTALLER_NAME);
77 assertThat(stringAtomicFormula.getValue()).isEqualTo(installerName);
78 assertThat(stringAtomicFormula.getIsHashedValue()).isFalse();
79 }
80
81 @Test
82 public void testValidAtomicFormula_stringValue_autoHash_longInstallerName() {
83 String installerName = "com.test.app.test.app.test.app.test.app.test.app";
84 StringAtomicFormula stringAtomicFormula =
85 new StringAtomicFormula(AtomicFormula.INSTALLER_NAME, installerName);
86
87 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.INSTALLER_NAME);
88 assertThat(stringAtomicFormula.getValue()).doesNotMatch(installerName);
89 assertThat(stringAtomicFormula.getIsHashedValue()).isTrue();
90 }
91
92 @Test
93 public void testValidAtomicFormula_stringValue_appCertificateAutoHashed() {
94 String appCert = "cert";
95 StringAtomicFormula stringAtomicFormula =
96 new StringAtomicFormula(AtomicFormula.APP_CERTIFICATE, appCert);
97
98 assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.APP_CERTIFICATE);
99 assertThat(stringAtomicFormula.getValue()).doesNotMatch(appCert);
100 assertThat(stringAtomicFormula.getIsHashedValue()).isTrue();
101 }
102
103 @Test
104 public void testValidAtomicFormula_stringValue_installerCertificateAutoHashed() {
105 String installerCert = "cert";
106 StringAtomicFormula stringAtomicFormula =
107 new StringAtomicFormula(AtomicFormula.INSTALLER_CERTIFICATE,
108 installerCert);
109
110 assertThat(stringAtomicFormula.getKey()).isEqualTo(
111 AtomicFormula.INSTALLER_CERTIFICATE);
112 assertThat(stringAtomicFormula.getValue()).doesNotMatch(installerCert);
113 assertThat(stringAtomicFormula.getIsHashedValue()).isTrue();
114 }
115
116 @Test
117 public void testValidAtomicFormula_longValue() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000118 LongAtomicFormula longAtomicFormula =
119 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000120 AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1);
121
122 assertThat(longAtomicFormula.getKey()).isEqualTo(AtomicFormula.VERSION_CODE);
123 assertThat(longAtomicFormula.getValue()).isEqualTo(1);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100124 }
125
126 @Test
127 public void testValidAtomicFormula_boolValue() {
Song Pan097f65d2019-11-10 18:02:52 +0000128 BooleanAtomicFormula atomicFormula =
129 new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100130
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000131 assertThat(atomicFormula.getKey()).isEqualTo(AtomicFormula.PRE_INSTALLED);
132 assertThat(atomicFormula.getValue()).isTrue();
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100133 }
134
135 @Test
136 public void testInvalidAtomicFormula_stringValue() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000137 Exception e =
138 expectThrows(
139 IllegalArgumentException.class,
140 () ->
141 new StringAtomicFormula(
142 AtomicFormula.VERSION_CODE,
143 "test-value",
144 /* isHashedValue= */ false));
145 assertThat(e.getMessage()).matches(
146 "Key VERSION_CODE cannot be used with StringAtomicFormula");
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100147 }
148
149 @Test
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000150 public void testInvalidAtomicFormula_longValue() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000151 Exception e =
152 expectThrows(
153 IllegalArgumentException.class,
154 () ->
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000155 new LongAtomicFormula(
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000156 AtomicFormula.PACKAGE_NAME, AtomicFormula.EQ, 1));
157 assertThat(e.getMessage()).matches(
158 "Key PACKAGE_NAME cannot be used with LongAtomicFormula");
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100159 }
160
161 @Test
162 public void testInvalidAtomicFormula_boolValue() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000163 Exception e =
164 expectThrows(
165 IllegalArgumentException.class,
166 () -> new BooleanAtomicFormula(AtomicFormula.PACKAGE_NAME, true));
167 assertThat(e.getMessage()).matches(
168 "Key PACKAGE_NAME cannot be used with BooleanAtomicFormula");
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100169 }
170
171 @Test
Song Pan097f65d2019-11-10 18:02:52 +0000172 public void testParcelUnparcel_string() {
Song Pan75147d52019-11-19 00:57:46 +0000173 StringAtomicFormula formula =
174 new StringAtomicFormula(
175 AtomicFormula.PACKAGE_NAME, "abc", /* isHashedValue= */ false);
Song Pan097f65d2019-11-10 18:02:52 +0000176 Parcel p = Parcel.obtain();
177 formula.writeToParcel(p, 0);
178 p.setDataPosition(0);
179 StringAtomicFormula newFormula = StringAtomicFormula.CREATOR.createFromParcel(p);
180
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000181 assertThat(newFormula).isEqualTo(formula);
Song Pan097f65d2019-11-10 18:02:52 +0000182 }
183
184 @Test
185 public void testParcelUnparcel_int() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000186 LongAtomicFormula formula =
187 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000188 AtomicFormula.VERSION_CODE, AtomicFormula.GT, 1);
Song Pan097f65d2019-11-10 18:02:52 +0000189 Parcel p = Parcel.obtain();
190 formula.writeToParcel(p, 0);
191 p.setDataPosition(0);
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000192 LongAtomicFormula newFormula =
193 LongAtomicFormula.CREATOR.createFromParcel(p);
Song Pan097f65d2019-11-10 18:02:52 +0000194
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000195 assertThat(newFormula).isEqualTo(formula);
Song Pan097f65d2019-11-10 18:02:52 +0000196 }
197
198 @Test
199 public void testParcelUnparcel_bool() {
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000200 BooleanAtomicFormula formula = new BooleanAtomicFormula(
201 AtomicFormula.PRE_INSTALLED, true);
Song Pan097f65d2019-11-10 18:02:52 +0000202 Parcel p = Parcel.obtain();
203 formula.writeToParcel(p, 0);
204 p.setDataPosition(0);
205 BooleanAtomicFormula newFormula = BooleanAtomicFormula.CREATOR.createFromParcel(p);
206
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000207 assertThat(newFormula).isEqualTo(formula);
Song Pan097f65d2019-11-10 18:02:52 +0000208 }
209
Khaled Abdelmohsen5809a5f2019-11-25 18:20:46 +0000210 @Test
211 public void testInvalidAtomicFormula_invalidKey() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000212 Exception e =
213 expectThrows(
214 IllegalArgumentException.class,
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000215 () -> new LongAtomicFormula(/* key= */ -1,
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000216 AtomicFormula.EQ, /* value= */0));
217 assertThat(e.getMessage()).matches("Unknown key: -1");
Khaled Abdelmohsen5809a5f2019-11-25 18:20:46 +0000218 }
219
220 @Test
221 public void testInvalidAtomicFormula_invalidOperator() {
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000222 Exception e =
223 expectThrows(
224 IllegalArgumentException.class,
225 () ->
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000226 new LongAtomicFormula(
Omer Nebil Yaverogluffc74352020-01-28 11:42:57 +0000227 AtomicFormula.VERSION_CODE, /* operator= */ -1, /* value= */
228 0));
229 assertThat(e.getMessage()).matches("Unknown operator: -1");
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000230 }
231
232 @Test
233 public void testFormulaMatches_string_true() {
234 StringAtomicFormula stringAtomicFormula =
235 new StringAtomicFormula(
236 AtomicFormula.PACKAGE_NAME, "com.test.app", /* isHashedValue= */
237 false);
238 AppInstallMetadata appInstallMetadata =
239 getAppInstallMetadataBuilder().setPackageName("com.test.app").build();
240
241 assertThat(stringAtomicFormula.matches(appInstallMetadata)).isTrue();
242 }
243
244 @Test
245 public void testFormulaMatches_string_false() {
246 StringAtomicFormula stringAtomicFormula =
247 new StringAtomicFormula(
248 AtomicFormula.PACKAGE_NAME, "com.test.app", /* isHashedValue= */
249 false);
250 AppInstallMetadata appInstallMetadata =
251 getAppInstallMetadataBuilder().setPackageName("com.foo.bar").build();
252
253 assertThat(stringAtomicFormula.matches(appInstallMetadata)).isFalse();
254 }
255
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000256
257 @Test
258 public void testIsAppCertificateFormula_string_true() {
259 StringAtomicFormula stringAtomicFormula =
260 new StringAtomicFormula(
261 AtomicFormula.APP_CERTIFICATE, "cert", /* isHashedValue= */false);
262
263 assertThat(stringAtomicFormula.isAppCertificateFormula()).isTrue();
264 }
265
266 @Test
267 public void testIsAppCertificateFormula_string_false() {
268 StringAtomicFormula stringAtomicFormula =
269 new StringAtomicFormula(
270 AtomicFormula.PACKAGE_NAME, "com.test.app", /* isHashedValue= */
271 false);
272
273 assertThat(stringAtomicFormula.isAppCertificateFormula()).isFalse();
274 }
275
276 @Test
277 public void testIsInstallerFormula_string_false() {
278 StringAtomicFormula stringAtomicFormula =
279 new StringAtomicFormula(
280 AtomicFormula.APP_CERTIFICATE, "cert", /* isHashedValue= */false);
281
282 assertThat(stringAtomicFormula.isInstallerFormula()).isFalse();
283 }
284
285 @Test
286 public void testIsInstallerFormula_string_installerName_true() {
287 StringAtomicFormula stringAtomicFormula =
288 new StringAtomicFormula(
289 AtomicFormula.INSTALLER_NAME,
290 "com.test.installer",
291 /* isHashedValue= */false);
292
293 assertThat(stringAtomicFormula.isInstallerFormula()).isTrue();
294 }
295
296 @Test
297 public void testIsInstallerFormula_string_installerCertificate_true() {
298 StringAtomicFormula stringAtomicFormula =
299 new StringAtomicFormula(
300 AtomicFormula.INSTALLER_CERTIFICATE, "cert", /* isHashedValue= */false);
301
302 assertThat(stringAtomicFormula.isInstallerFormula()).isTrue();
303 }
304
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000305 @Test
306 public void testFormulaMatches_long_eq_true() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000307 LongAtomicFormula longAtomicFormula =
308 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000309 AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 0);
310 AppInstallMetadata appInstallMetadata =
311 getAppInstallMetadataBuilder().setVersionCode(0).build();
312
313 assertThat(longAtomicFormula.matches(appInstallMetadata)).isTrue();
314 }
315
316 @Test
317 public void testFormulaMatches_long_eq_false() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000318 LongAtomicFormula longAtomicFormula =
319 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000320 AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 0);
321 AppInstallMetadata appInstallMetadata =
322 getAppInstallMetadataBuilder().setVersionCode(1).build();
323
324 assertThat(longAtomicFormula.matches(appInstallMetadata)).isFalse();
325 }
326
327 @Test
328 public void testFormulaMatches_long_gt_true() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000329 LongAtomicFormula longAtomicFormula =
330 new LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.GT,
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000331 0);
332 AppInstallMetadata appInstallMetadata =
333 getAppInstallMetadataBuilder().setVersionCode(1).build();
334
335 assertThat(longAtomicFormula.matches(appInstallMetadata)).isTrue();
336 }
337
338 @Test
339 public void testFormulaMatches_long_gt_false() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000340 LongAtomicFormula longAtomicFormula =
341 new LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.GT,
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000342 1);
343 AppInstallMetadata appInstallMetadata =
344 getAppInstallMetadataBuilder().setVersionCode(0).build();
345
346 assertThat(longAtomicFormula.matches(appInstallMetadata)).isFalse();
347 }
348
349 @Test
350 public void testFormulaMatches_long_gte_true() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000351 LongAtomicFormula longAtomicFormula =
352 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000353 AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1);
354
355 AppInstallMetadata appInstallMetadata1 =
356 getAppInstallMetadataBuilder().setVersionCode(1).build();
357 assertThat(longAtomicFormula.matches(appInstallMetadata1)).isTrue();
358
359 AppInstallMetadata appInstallMetadata2 =
360 getAppInstallMetadataBuilder().setVersionCode(2).build();
361 assertThat(longAtomicFormula.matches(appInstallMetadata2)).isTrue();
362 }
363
364 @Test
365 public void testFormulaMatches_long_gte_false() {
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000366 LongAtomicFormula longAtomicFormula =
367 new LongAtomicFormula(
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000368 AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1);
369 AppInstallMetadata appInstallMetadata =
370 getAppInstallMetadataBuilder().setVersionCode(0).build();
371
372 assertThat(longAtomicFormula.matches(appInstallMetadata)).isFalse();
373 }
374
375 @Test
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000376 public void testIsAppCertificateFormula_long_false() {
377 LongAtomicFormula longAtomicFormula =
378 new AtomicFormula.LongAtomicFormula(
379 AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1);
380
381 assertThat(longAtomicFormula.isAppCertificateFormula()).isFalse();
382 }
383
384 @Test
385 public void testIsInstallerFormula_long_false() {
386 LongAtomicFormula longAtomicFormula =
387 new LongAtomicFormula(
388 AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1);
389
390 assertThat(longAtomicFormula.isInstallerFormula()).isFalse();
391 }
392
393 @Test
Omer Nebil Yaveroglu15395f52020-01-22 12:14:44 +0000394 public void testFormulaMatches_bool_true() {
395 BooleanAtomicFormula boolFormula =
396 new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true);
397 AppInstallMetadata appInstallMetadata =
398 getAppInstallMetadataBuilder().setIsPreInstalled(true).build();
399
400 assertThat(boolFormula.matches(appInstallMetadata)).isTrue();
401 }
402
403 @Test
404 public void testFormulaMatches_bool_false() {
405 BooleanAtomicFormula boolFormula =
406 new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true);
407 AppInstallMetadata appInstallMetadata =
408 getAppInstallMetadataBuilder().setIsPreInstalled(false).build();
409
410 assertThat(boolFormula.matches(appInstallMetadata)).isFalse();
Khaled Abdelmohsen5809a5f2019-11-25 18:20:46 +0000411 }
412
Omer Nebil Yaveroglu84f7c3f2020-01-29 12:18:10 +0000413 @Test
414 public void testIsAppCertificateFormula_bool_false() {
415 BooleanAtomicFormula boolFormula =
416 new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true);
417
418 assertThat(boolFormula.isAppCertificateFormula()).isFalse();
419 }
420
421 @Test
422 public void testIsInstallerFormula_bool_false() {
423 BooleanAtomicFormula boolFormula =
424 new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true);
425
426 assertThat(boolFormula.isInstallerFormula()).isFalse();
427 }
428
Song Pan097f65d2019-11-10 18:02:52 +0000429 /** Returns a builder with all fields filled with some dummy data. */
430 private AppInstallMetadata.Builder getAppInstallMetadataBuilder() {
431 return new AppInstallMetadata.Builder()
432 .setPackageName("abc")
433 .setAppCertificate("abc")
434 .setInstallerCertificate("abc")
435 .setInstallerName("abc")
436 .setVersionCode(-1)
437 .setIsPreInstalled(true);
Khaled Abdelmohsenbe0454d2019-10-07 10:24:18 +0100438 }
439}