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; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 20 | |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 21 | import static org.testng.Assert.expectThrows; |
| 22 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 23 | import android.content.integrity.AtomicFormula.BooleanAtomicFormula; |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 24 | import android.content.integrity.AtomicFormula.LongAtomicFormula; |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 25 | import android.content.integrity.AtomicFormula.StringAtomicFormula; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | @RunWith(JUnit4.class) |
| 33 | public class AtomicFormulaTest { |
| 34 | |
| 35 | @Test |
| 36 | public void testValidAtomicFormula_stringValue() { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 37 | String packageName = "com.test.app"; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 38 | StringAtomicFormula stringAtomicFormula = |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 39 | new StringAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 40 | AtomicFormula.PACKAGE_NAME, packageName, /* isHashedValue= */false); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 41 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 42 | assertThat(stringAtomicFormula.getKey()).isEqualTo(AtomicFormula.PACKAGE_NAME); |
| 43 | assertThat(stringAtomicFormula.getValue()).isEqualTo(packageName); |
| 44 | assertThat(stringAtomicFormula.getIsHashedValue()).isFalse(); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Test |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 48 | public void testValidAtomicFormula_stringValue_autoHash_packageNameLessThanLimit() { |
| 49 | String packageName = "com.test.app"; |
| 50 | StringAtomicFormula stringAtomicFormula = |
| 51 | new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, packageName); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 52 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 53 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 118 | LongAtomicFormula longAtomicFormula = |
| 119 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 120 | AtomicFormula.VERSION_CODE, AtomicFormula.GTE, 1); |
| 121 | |
| 122 | assertThat(longAtomicFormula.getKey()).isEqualTo(AtomicFormula.VERSION_CODE); |
| 123 | assertThat(longAtomicFormula.getValue()).isEqualTo(1); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testValidAtomicFormula_boolValue() { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 128 | BooleanAtomicFormula atomicFormula = |
| 129 | new BooleanAtomicFormula(AtomicFormula.PRE_INSTALLED, true); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 130 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 131 | assertThat(atomicFormula.getKey()).isEqualTo(AtomicFormula.PRE_INSTALLED); |
| 132 | assertThat(atomicFormula.getValue()).isTrue(); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testInvalidAtomicFormula_stringValue() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 137 | 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 Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Test |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 150 | public void testInvalidAtomicFormula_longValue() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 151 | Exception e = |
| 152 | expectThrows( |
| 153 | IllegalArgumentException.class, |
| 154 | () -> |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 155 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 156 | AtomicFormula.PACKAGE_NAME, AtomicFormula.EQ, 1)); |
| 157 | assertThat(e.getMessage()).matches( |
| 158 | "Key PACKAGE_NAME cannot be used with LongAtomicFormula"); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testInvalidAtomicFormula_boolValue() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 163 | 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 Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 172 | public void testParcelUnparcel_string() { |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 173 | StringAtomicFormula formula = |
| 174 | new StringAtomicFormula( |
| 175 | AtomicFormula.PACKAGE_NAME, "abc", /* isHashedValue= */ false); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 176 | Parcel p = Parcel.obtain(); |
| 177 | formula.writeToParcel(p, 0); |
| 178 | p.setDataPosition(0); |
| 179 | StringAtomicFormula newFormula = StringAtomicFormula.CREATOR.createFromParcel(p); |
| 180 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 181 | assertThat(newFormula).isEqualTo(formula); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testParcelUnparcel_int() { |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 186 | LongAtomicFormula formula = |
| 187 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 188 | AtomicFormula.VERSION_CODE, AtomicFormula.GT, 1); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 189 | Parcel p = Parcel.obtain(); |
| 190 | formula.writeToParcel(p, 0); |
| 191 | p.setDataPosition(0); |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 192 | LongAtomicFormula newFormula = |
| 193 | LongAtomicFormula.CREATOR.createFromParcel(p); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 194 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 195 | assertThat(newFormula).isEqualTo(formula); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testParcelUnparcel_bool() { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 200 | BooleanAtomicFormula formula = new BooleanAtomicFormula( |
| 201 | AtomicFormula.PRE_INSTALLED, true); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 202 | Parcel p = Parcel.obtain(); |
| 203 | formula.writeToParcel(p, 0); |
| 204 | p.setDataPosition(0); |
| 205 | BooleanAtomicFormula newFormula = BooleanAtomicFormula.CREATOR.createFromParcel(p); |
| 206 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 207 | assertThat(newFormula).isEqualTo(formula); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 210 | @Test |
| 211 | public void testInvalidAtomicFormula_invalidKey() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 212 | Exception e = |
| 213 | expectThrows( |
| 214 | IllegalArgumentException.class, |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 215 | () -> new LongAtomicFormula(/* key= */ -1, |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 216 | AtomicFormula.EQ, /* value= */0)); |
| 217 | assertThat(e.getMessage()).matches("Unknown key: -1"); |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testInvalidAtomicFormula_invalidOperator() { |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 222 | Exception e = |
| 223 | expectThrows( |
| 224 | IllegalArgumentException.class, |
| 225 | () -> |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 226 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | ffc7435 | 2020-01-28 11:42:57 +0000 | [diff] [blame] | 227 | AtomicFormula.VERSION_CODE, /* operator= */ -1, /* value= */ |
| 228 | 0)); |
| 229 | assertThat(e.getMessage()).matches("Unknown operator: -1"); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 230 | } |
| 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 256 | |
| 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 Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 305 | @Test |
| 306 | public void testFormulaMatches_long_eq_true() { |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 307 | LongAtomicFormula longAtomicFormula = |
| 308 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 309 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 318 | LongAtomicFormula longAtomicFormula = |
| 319 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 320 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 329 | LongAtomicFormula longAtomicFormula = |
| 330 | new LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.GT, |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 331 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 340 | LongAtomicFormula longAtomicFormula = |
| 341 | new LongAtomicFormula(AtomicFormula.VERSION_CODE, AtomicFormula.GT, |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 342 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 351 | LongAtomicFormula longAtomicFormula = |
| 352 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 353 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 366 | LongAtomicFormula longAtomicFormula = |
| 367 | new LongAtomicFormula( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 368 | 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 Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 376 | 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 Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 394 | 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 Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 413 | @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 Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 429 | /** 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 Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 438 | } |
| 439 | } |