Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 1 | /* |
| 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 Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 17 | package android.content.integrity; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 18 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 21 | import static org.testng.Assert.expectThrows; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 22 | |
| 23 | import android.os.Parcel; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 24 | |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
Khaled Abdelmohsen | a4477fa | 2019-10-09 14:35:32 +0100 | [diff] [blame] | 29 | import java.util.Arrays; |
| 30 | import java.util.Collections; |
| 31 | |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 32 | @RunWith(JUnit4.class) |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 33 | public class CompoundFormulaTest { |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 34 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 35 | private static final AtomicFormula ATOMIC_FORMULA_1 = |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 36 | new AtomicFormula.StringAtomicFormula( |
| 37 | AtomicFormula.PACKAGE_NAME, "test1", /* isHashedValue= */ false); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 38 | private static final AtomicFormula ATOMIC_FORMULA_2 = |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 39 | new AtomicFormula.LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.EQ, 1); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 40 | |
| 41 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 42 | public void testValidCompoundFormula() { |
| 43 | CompoundFormula compoundFormula = |
| 44 | new CompoundFormula( |
| 45 | CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 46 | |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 47 | assertThat(compoundFormula.getConnector()).isEqualTo(CompoundFormula.AND); |
| 48 | assertThat(compoundFormula.getFormulas()).containsAllOf(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testValidateAuxiliaryFormula_binaryConnectors() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 53 | 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 Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testValidateAuxiliaryFormula_unaryConnectors() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 65 | 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 Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 76 | public void testParcelUnparcel() { |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 77 | CompoundFormula formula = |
| 78 | new CompoundFormula( |
| 79 | CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_2, ATOMIC_FORMULA_1)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 80 | Parcel p = Parcel.obtain(); |
| 81 | formula.writeToParcel(p, 0); |
| 82 | p.setDataPosition(0); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 83 | |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 84 | assertThat(CompoundFormula.CREATOR.createFromParcel(p)).isEqualTo(formula); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 87 | @Test |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 88 | public void testInvalidCompoundFormula_invalidConnector() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 89 | 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 Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 99 | @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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 228 | @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 Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 285 | /** 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 Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 294 | } |
| 295 | } |