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 17 package com.android.server.compat; 18 19 import static com.android.internal.compat.OverrideAllowedState.ALLOWED; 20 import static com.android.internal.compat.OverrideAllowedState.DEFERRED_VERIFICATION; 21 import static com.android.internal.compat.OverrideAllowedState.DISABLED_NON_TARGET_SDK; 22 import static com.android.internal.compat.OverrideAllowedState.DISABLED_NOT_DEBUGGABLE; 23 import static com.android.internal.compat.OverrideAllowedState.DISABLED_TARGET_SDK_TOO_HIGH; 24 import static com.android.internal.compat.OverrideAllowedState.LOGGING_ONLY_CHANGE; 25 import static com.android.internal.compat.OverrideAllowedState.PLATFORM_TOO_OLD; 26 27 import static com.google.common.truth.Truth.assertThat; 28 29 import static org.mockito.ArgumentMatchers.anyInt; 30 import static org.mockito.ArgumentMatchers.eq; 31 import static org.mockito.Mockito.mock; 32 import static org.mockito.Mockito.when; 33 34 import android.content.Context; 35 import android.content.pm.PackageManager; 36 import android.content.pm.PackageManager.NameNotFoundException; 37 38 import androidx.test.runner.AndroidJUnit4; 39 40 import com.android.internal.compat.AndroidBuildClassifier; 41 import com.android.internal.compat.IOverrideValidator; 42 import com.android.internal.compat.OverrideAllowedState; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 50 @RunWith(AndroidJUnit4.class) 51 public class OverrideValidatorImplTest { 52 private static final String PACKAGE_NAME = "my.package"; 53 private static final int TARGET_SDK = 10; 54 private static final int TARGET_SDK_BEFORE = 9; 55 private static final int TARGET_SDK_AFTER = 11; 56 private static final int PLATFORM_SDK_VERSION = 30; 57 58 @Mock 59 private PackageManager mPackageManager; 60 @Mock 61 Context mContext; 62 debuggableBuild()63 private AndroidBuildClassifier debuggableBuild() { 64 AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class); 65 when(buildClassifier.isDebuggableBuild()).thenReturn(true); 66 when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION); 67 return buildClassifier; 68 } 69 betaBuild()70 private AndroidBuildClassifier betaBuild() { 71 AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class); 72 when(buildClassifier.isDebuggableBuild()).thenReturn(false); 73 when(buildClassifier.isFinalBuild()).thenReturn(false); 74 when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION); 75 return buildClassifier; 76 } 77 finalBuild()78 private AndroidBuildClassifier finalBuild() { 79 AndroidBuildClassifier buildClassifier = mock(AndroidBuildClassifier.class); 80 when(buildClassifier.isDebuggableBuild()).thenReturn(false); 81 when(buildClassifier.isFinalBuild()).thenReturn(true); 82 when(buildClassifier.platformTargetSdk()).thenReturn(PLATFORM_SDK_VERSION); 83 return buildClassifier; 84 } 85 86 @Before setUp()87 public void setUp() throws Exception { 88 MockitoAnnotations.initMocks(this); 89 android.app.compat.ChangeIdStateCache.disable(); 90 when(mContext.getPackageManager()).thenReturn(mPackageManager); 91 } 92 93 @Test getOverrideAllowedState_debugBuildAnyChangeDebugApp_allowOverride()94 public void getOverrideAllowedState_debugBuildAnyChangeDebugApp_allowOverride() 95 throws Exception { 96 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 97 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 98 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 99 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 100 .addEnabledChangeWithId(4) 101 .addDisabledChangeWithId(5) 102 .addLoggingOnlyChangeWithId(6).build(); 103 IOverrideValidator overrideValidator = config.getOverrideValidator(); 104 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 105 .thenReturn(ApplicationInfoBuilder.create() 106 .debuggable() 107 .withTargetSdk(TARGET_SDK) 108 .withPackageName(PACKAGE_NAME).build()); 109 110 OverrideAllowedState stateTargetSdkLessChange = 111 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 112 OverrideAllowedState stateTargetSdkEqualChange = 113 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 114 OverrideAllowedState stateTargetSdkAfterChange = 115 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 116 OverrideAllowedState stateEnabledChange = 117 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 118 OverrideAllowedState stateDisabledChange = 119 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 120 OverrideAllowedState stateDLoggingOnlyChange = 121 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 122 123 assertThat(stateTargetSdkLessChange) 124 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 125 assertThat(stateTargetSdkEqualChange) 126 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 127 assertThat(stateTargetSdkAfterChange) 128 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 129 assertThat(stateEnabledChange) 130 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 131 assertThat(stateDisabledChange) 132 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 133 assertThat(stateDLoggingOnlyChange) 134 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 135 } 136 137 @Test getOverrideAllowedState_debugBuildAnyChangeReleaseApp_allowOverride()138 public void getOverrideAllowedState_debugBuildAnyChangeReleaseApp_allowOverride() 139 throws Exception { 140 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 141 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 142 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 143 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 144 .addEnabledChangeWithId(4) 145 .addDisabledChangeWithId(5) 146 .addLoggingOnlyChangeWithId(6).build(); 147 IOverrideValidator overrideValidator = config.getOverrideValidator(); 148 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 149 .thenReturn(ApplicationInfoBuilder.create() 150 .withPackageName(PACKAGE_NAME) 151 .withTargetSdk(TARGET_SDK).build()); 152 153 OverrideAllowedState stateTargetSdkLessChange = 154 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 155 OverrideAllowedState stateTargetSdkEqualChange = 156 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 157 OverrideAllowedState stateTargetSdkAfterChange = 158 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 159 OverrideAllowedState stateEnabledChange = 160 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 161 OverrideAllowedState stateDisabledChange = 162 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 163 OverrideAllowedState stateDLoggingOnlyChange = 164 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 165 166 assertThat(stateTargetSdkLessChange) 167 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 168 assertThat(stateTargetSdkEqualChange) 169 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 170 assertThat(stateTargetSdkAfterChange) 171 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 172 assertThat(stateEnabledChange) 173 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 174 assertThat(stateDisabledChange) 175 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 176 assertThat(stateDLoggingOnlyChange) 177 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 178 } 179 180 @Test getOverrideAllowedState_betaBuildTargetSdkChangeDebugApp_allowOverride()181 public void getOverrideAllowedState_betaBuildTargetSdkChangeDebugApp_allowOverride() 182 throws Exception { 183 CompatConfig config = CompatConfigBuilder.create(betaBuild(), mContext) 184 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 185 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 186 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 187 .addDisabledChangeWithId(4).build(); 188 IOverrideValidator overrideValidator = config.getOverrideValidator(); 189 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 190 .thenReturn(ApplicationInfoBuilder.create() 191 .debuggable() 192 .withTargetSdk(TARGET_SDK) 193 .withPackageName(PACKAGE_NAME).build()); 194 195 OverrideAllowedState stateTargetSdkLessChange = 196 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 197 OverrideAllowedState stateTargetSdkEqualChange = 198 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 199 OverrideAllowedState stateTargetSdkAfterChange = 200 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 201 OverrideAllowedState stateDisabledChange = 202 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 203 204 assertThat(stateTargetSdkLessChange) 205 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK_BEFORE)); 206 assertThat(stateTargetSdkEqualChange) 207 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK)); 208 assertThat(stateTargetSdkAfterChange) 209 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK_AFTER)); 210 assertThat(stateDisabledChange) 211 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, -1)); 212 } 213 214 @Test getOverrideAllowedState_betaBuildEnabledChangeDebugApp_allowOverride()215 public void getOverrideAllowedState_betaBuildEnabledChangeDebugApp_allowOverride() 216 throws Exception { 217 CompatConfig config = CompatConfigBuilder.create(betaBuild(), mContext) 218 .addEnabledChangeWithId(1).build(); 219 IOverrideValidator overrideValidator = config.getOverrideValidator(); 220 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 221 .thenReturn(ApplicationInfoBuilder.create() 222 .withPackageName(PACKAGE_NAME) 223 .debuggable() 224 .build()); 225 226 OverrideAllowedState allowedState = 227 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 228 229 assertThat(allowedState) 230 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 231 } 232 233 @Test getOverrideAllowedState_betaBuildDisabledChangeDebugApp_allowOverride()234 public void getOverrideAllowedState_betaBuildDisabledChangeDebugApp_allowOverride() 235 throws Exception { 236 CompatConfig config = CompatConfigBuilder.create(betaBuild(), mContext) 237 .addDisabledChangeWithId(1).build(); 238 IOverrideValidator overrideValidator = config.getOverrideValidator(); 239 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 240 .thenReturn(ApplicationInfoBuilder.create() 241 .debuggable() 242 .withPackageName(PACKAGE_NAME).build()); 243 244 OverrideAllowedState allowedState = 245 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 246 247 assertThat(allowedState) 248 .isEqualTo(new OverrideAllowedState(ALLOWED, -1, -1)); 249 } 250 251 @Test getOverrideAllowedState_betaBuildAnyChangeReleaseApp_rejectOverride()252 public void getOverrideAllowedState_betaBuildAnyChangeReleaseApp_rejectOverride() 253 throws Exception { 254 CompatConfig config = CompatConfigBuilder.create(betaBuild(), mContext) 255 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 256 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 257 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 258 .addEnabledChangeWithId(4) 259 .addDisabledChangeWithId(5) 260 .addLoggingOnlyChangeWithId(6).build(); 261 IOverrideValidator overrideValidator = config.getOverrideValidator(); 262 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 263 .thenReturn(ApplicationInfoBuilder.create() 264 .withPackageName(PACKAGE_NAME) 265 .withTargetSdk(TARGET_SDK).build()); 266 267 OverrideAllowedState stateTargetSdkLessChange = 268 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 269 OverrideAllowedState stateTargetSdkEqualChange = 270 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 271 OverrideAllowedState stateTargetSdkAfterChange = 272 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 273 OverrideAllowedState stateEnabledChange = 274 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 275 OverrideAllowedState stateDisabledChange = 276 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 277 OverrideAllowedState stateDLoggingOnlyChange = 278 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 279 280 assertThat(stateTargetSdkLessChange) 281 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 282 assertThat(stateTargetSdkEqualChange) 283 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 284 assertThat(stateTargetSdkAfterChange) 285 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 286 assertThat(stateEnabledChange) 287 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 288 assertThat(stateDisabledChange) 289 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 290 assertThat(stateDLoggingOnlyChange) 291 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 292 } 293 294 @Test getOverrideAllowedState_finalBuildTargetSdkChangeDebugAppOptin_allowOverride()295 public void getOverrideAllowedState_finalBuildTargetSdkChangeDebugAppOptin_allowOverride() 296 throws Exception { 297 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 298 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 1) 299 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2).build(); 300 IOverrideValidator overrideValidator = config.getOverrideValidator(); 301 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 302 .thenReturn(ApplicationInfoBuilder.create() 303 .debuggable() 304 .withTargetSdk(TARGET_SDK) 305 .withPackageName(PACKAGE_NAME).build()); 306 307 OverrideAllowedState stateTargetSdkGreaterChange = 308 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 309 OverrideAllowedState stateTargetSdkEqualChange = 310 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 311 312 assertThat(stateTargetSdkGreaterChange) 313 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK_AFTER)); 314 315 assertThat(stateTargetSdkEqualChange) 316 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK)); 317 } 318 319 @Test getOverrideAllowedState_finalBuildTargetSdkChangeDebugAppOptout_rejectOverride()320 public void getOverrideAllowedState_finalBuildTargetSdkChangeDebugAppOptout_rejectOverride() 321 throws Exception { 322 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 323 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1).build(); 324 IOverrideValidator overrideValidator = config.getOverrideValidator(); 325 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 326 .thenReturn(ApplicationInfoBuilder.create() 327 .withPackageName(PACKAGE_NAME) 328 .withTargetSdk(TARGET_SDK) 329 .debuggable() 330 .build()); 331 332 OverrideAllowedState stateTargetSdkLessChange = 333 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 334 335 assertThat(stateTargetSdkLessChange).isEqualTo( 336 new OverrideAllowedState(DISABLED_TARGET_SDK_TOO_HIGH, TARGET_SDK, 337 TARGET_SDK_BEFORE)); 338 } 339 340 @Test getOverrideAllowedState_targetSdkChangeGreaterThanOsVersion_rejectOverride()341 public void getOverrideAllowedState_targetSdkChangeGreaterThanOsVersion_rejectOverride() 342 throws Exception { 343 final AndroidBuildClassifier buildClassifier = finalBuild(); 344 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 345 .addEnabledSinceApexChangeWithId(PLATFORM_SDK_VERSION + 1, 1).build(); 346 IOverrideValidator overrideValidator = config.getOverrideValidator(); 347 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 348 .thenReturn(ApplicationInfoBuilder.create() 349 .withPackageName(PACKAGE_NAME) 350 .debuggable() 351 .build()); 352 353 OverrideAllowedState stateTargetSdkLessChange = 354 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 355 assertThat(stateTargetSdkLessChange).isEqualTo( 356 new OverrideAllowedState(PLATFORM_TOO_OLD, -1, 357 PLATFORM_SDK_VERSION)); 358 } 359 360 @Test getOverrideAllowedState_finalBuildEnabledChangeDebugApp_rejectOverride()361 public void getOverrideAllowedState_finalBuildEnabledChangeDebugApp_rejectOverride() 362 throws Exception { 363 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 364 .addEnabledChangeWithId(1).build(); 365 IOverrideValidator overrideValidator = config.getOverrideValidator(); 366 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 367 .thenReturn(ApplicationInfoBuilder.create() 368 .withPackageName(PACKAGE_NAME) 369 .debuggable().build()); 370 371 OverrideAllowedState allowedState = 372 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 373 374 assertThat(allowedState) 375 .isEqualTo(new OverrideAllowedState(DISABLED_NON_TARGET_SDK, -1, -1)); 376 } 377 378 @Test getOverrideAllowedState_finalBuildDisabledChangeDebugApp_allowOverride()379 public void getOverrideAllowedState_finalBuildDisabledChangeDebugApp_allowOverride() 380 throws Exception { 381 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 382 .addDisabledChangeWithId(1).build(); 383 IOverrideValidator overrideValidator = config.getOverrideValidator(); 384 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 385 .thenReturn(ApplicationInfoBuilder.create() 386 .withPackageName(PACKAGE_NAME) 387 .withTargetSdk(TARGET_SDK) 388 .debuggable().build()); 389 390 OverrideAllowedState allowedState = 391 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 392 393 assertThat(allowedState) 394 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, -1)); 395 } 396 397 @Test getOverrideAllowedState_finalBuildAnyChangeReleaseApp_rejectOverride()398 public void getOverrideAllowedState_finalBuildAnyChangeReleaseApp_rejectOverride() 399 throws Exception { 400 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 401 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 402 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 403 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 404 .addEnabledChangeWithId(4) 405 .addDisabledChangeWithId(5) 406 .addLoggingOnlyChangeWithId(6).build(); 407 IOverrideValidator overrideValidator = config.getOverrideValidator(); 408 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 409 .thenReturn(ApplicationInfoBuilder.create() 410 .withPackageName(PACKAGE_NAME) 411 .withTargetSdk(TARGET_SDK).build()); 412 413 OverrideAllowedState stateTargetSdkLessChange = 414 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 415 OverrideAllowedState stateTargetSdkEqualChange = 416 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 417 OverrideAllowedState stateTargetSdkAfterChange = 418 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 419 OverrideAllowedState stateEnabledChange = 420 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 421 OverrideAllowedState stateDisabledChange = 422 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 423 OverrideAllowedState stateDLoggingOnlyChange = 424 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 425 426 assertThat(stateTargetSdkLessChange) 427 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 428 assertThat(stateTargetSdkEqualChange) 429 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 430 assertThat(stateTargetSdkAfterChange) 431 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 432 assertThat(stateEnabledChange) 433 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 434 assertThat(stateDisabledChange) 435 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 436 assertThat(stateDLoggingOnlyChange) 437 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 438 } 439 @Test getOverrideAllowedState_finalBuildAnyChangeNotInstalledApp_deferOverride()440 public void getOverrideAllowedState_finalBuildAnyChangeNotInstalledApp_deferOverride() 441 throws Exception { 442 CompatConfig config = CompatConfigBuilder.create(finalBuild(), mContext) 443 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 444 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 445 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 446 .addEnabledChangeWithId(4) 447 .addDisabledChangeWithId(5) 448 .addLoggingOnlyChangeWithId(6).build(); 449 IOverrideValidator overrideValidator = config.getOverrideValidator(); 450 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 451 .thenThrow(new NameNotFoundException()); 452 453 OverrideAllowedState stateTargetSdkLessChange = 454 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 455 OverrideAllowedState stateTargetSdkEqualChange = 456 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 457 OverrideAllowedState stateTargetSdkAfterChange = 458 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 459 OverrideAllowedState stateEnabledChange = 460 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 461 OverrideAllowedState stateDisabledChange = 462 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 463 OverrideAllowedState stateDLoggingOnlyChange = 464 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 465 466 assertThat(stateTargetSdkLessChange) 467 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 468 assertThat(stateTargetSdkEqualChange) 469 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 470 assertThat(stateTargetSdkAfterChange) 471 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 472 assertThat(stateEnabledChange) 473 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 474 assertThat(stateDisabledChange) 475 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 476 assertThat(stateDLoggingOnlyChange) 477 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 478 } 479 480 @Test getOverrideAllowedState_forceFinalBuildTargetSdkChangeDebugAppOptin_allowOverride()481 public void getOverrideAllowedState_forceFinalBuildTargetSdkChangeDebugAppOptin_allowOverride() 482 throws Exception { 483 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 484 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 1) 485 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2).build(); 486 config.forceNonDebuggableFinalForTest(true); 487 IOverrideValidator overrideValidator = config.getOverrideValidator(); 488 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 489 .thenReturn(ApplicationInfoBuilder.create() 490 .debuggable() 491 .withTargetSdk(TARGET_SDK) 492 .withPackageName(PACKAGE_NAME).build()); 493 494 OverrideAllowedState stateTargetSdkGreaterChange = 495 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 496 OverrideAllowedState stateTargetSdkEqualChange = 497 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 498 499 assertThat(stateTargetSdkGreaterChange) 500 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK_AFTER)); 501 502 assertThat(stateTargetSdkEqualChange) 503 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, TARGET_SDK)); 504 } 505 506 @Test getOverrideAllowedState_forceFinalBldTargetSdkChangeDebugAppOptout_rejectOverride()507 public void getOverrideAllowedState_forceFinalBldTargetSdkChangeDebugAppOptout_rejectOverride() 508 throws Exception { 509 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 510 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1).build(); 511 config.forceNonDebuggableFinalForTest(true); 512 IOverrideValidator overrideValidator = config.getOverrideValidator(); 513 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 514 .thenReturn(ApplicationInfoBuilder.create() 515 .withPackageName(PACKAGE_NAME) 516 .withTargetSdk(TARGET_SDK) 517 .debuggable() 518 .build()); 519 520 OverrideAllowedState stateTargetSdkLessChange = 521 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 522 523 assertThat(stateTargetSdkLessChange).isEqualTo( 524 new OverrideAllowedState(DISABLED_TARGET_SDK_TOO_HIGH, TARGET_SDK, 525 TARGET_SDK_BEFORE)); 526 } 527 528 @Test getOverrideAllowedState_forceFinalBuildEnabledChangeDebugApp_rejectOverride()529 public void getOverrideAllowedState_forceFinalBuildEnabledChangeDebugApp_rejectOverride() 530 throws Exception { 531 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 532 .addEnabledChangeWithId(1).build(); 533 config.forceNonDebuggableFinalForTest(true); 534 IOverrideValidator overrideValidator = config.getOverrideValidator(); 535 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 536 .thenReturn(ApplicationInfoBuilder.create() 537 .withPackageName(PACKAGE_NAME) 538 .debuggable().build()); 539 540 OverrideAllowedState allowedState = 541 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 542 543 assertThat(allowedState) 544 .isEqualTo(new OverrideAllowedState(DISABLED_NON_TARGET_SDK, -1, -1)); 545 } 546 547 @Test getOverrideAllowedState_forceFinalBuildDisabledChangeDebugApp_allowOverride()548 public void getOverrideAllowedState_forceFinalBuildDisabledChangeDebugApp_allowOverride() 549 throws Exception { 550 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 551 .addDisabledChangeWithId(1).build(); 552 config.forceNonDebuggableFinalForTest(true); 553 IOverrideValidator overrideValidator = config.getOverrideValidator(); 554 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 555 .thenReturn(ApplicationInfoBuilder.create() 556 .withPackageName(PACKAGE_NAME) 557 .withTargetSdk(TARGET_SDK) 558 .debuggable().build()); 559 560 OverrideAllowedState allowedState = 561 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 562 563 assertThat(allowedState) 564 .isEqualTo(new OverrideAllowedState(ALLOWED, TARGET_SDK, -1)); 565 } 566 567 @Test getOverrideAllowedState_forceFinalBuildAnyChangeReleaseApp_rejectOverride()568 public void getOverrideAllowedState_forceFinalBuildAnyChangeReleaseApp_rejectOverride() 569 throws Exception { 570 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 571 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 572 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 573 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 574 .addEnabledChangeWithId(4) 575 .addDisabledChangeWithId(5) 576 .addLoggingOnlyChangeWithId(6).build(); 577 config.forceNonDebuggableFinalForTest(true); 578 IOverrideValidator overrideValidator = config.getOverrideValidator(); 579 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 580 .thenReturn(ApplicationInfoBuilder.create() 581 .withPackageName(PACKAGE_NAME) 582 .withTargetSdk(TARGET_SDK).build()); 583 584 OverrideAllowedState stateTargetSdkLessChange = 585 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 586 OverrideAllowedState stateTargetSdkEqualChange = 587 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 588 OverrideAllowedState stateTargetSdkAfterChange = 589 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 590 OverrideAllowedState stateEnabledChange = 591 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 592 OverrideAllowedState stateDisabledChange = 593 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 594 OverrideAllowedState stateDLoggingOnlyChange = 595 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 596 597 assertThat(stateTargetSdkLessChange) 598 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 599 assertThat(stateTargetSdkEqualChange) 600 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 601 assertThat(stateTargetSdkAfterChange) 602 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 603 assertThat(stateEnabledChange) 604 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 605 assertThat(stateDisabledChange) 606 .isEqualTo(new OverrideAllowedState(DISABLED_NOT_DEBUGGABLE, -1, -1)); 607 assertThat(stateDLoggingOnlyChange) 608 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 609 } 610 @Test getOverrideAllowedState_forceFinalBuildAnyChangeNotInstalledApp_deferOverride()611 public void getOverrideAllowedState_forceFinalBuildAnyChangeNotInstalledApp_deferOverride() 612 throws Exception { 613 CompatConfig config = CompatConfigBuilder.create(debuggableBuild(), mContext) 614 .addEnableAfterSdkChangeWithId(TARGET_SDK_BEFORE, 1) 615 .addEnableAfterSdkChangeWithId(TARGET_SDK, 2) 616 .addEnableAfterSdkChangeWithId(TARGET_SDK_AFTER, 3) 617 .addEnabledChangeWithId(4) 618 .addDisabledChangeWithId(5) 619 .addLoggingOnlyChangeWithId(6).build(); 620 config.forceNonDebuggableFinalForTest(true); 621 IOverrideValidator overrideValidator = config.getOverrideValidator(); 622 when(mPackageManager.getApplicationInfo(eq(PACKAGE_NAME), anyInt())) 623 .thenThrow(new NameNotFoundException()); 624 625 OverrideAllowedState stateTargetSdkLessChange = 626 overrideValidator.getOverrideAllowedState(1, PACKAGE_NAME); 627 OverrideAllowedState stateTargetSdkEqualChange = 628 overrideValidator.getOverrideAllowedState(2, PACKAGE_NAME); 629 OverrideAllowedState stateTargetSdkAfterChange = 630 overrideValidator.getOverrideAllowedState(3, PACKAGE_NAME); 631 OverrideAllowedState stateEnabledChange = 632 overrideValidator.getOverrideAllowedState(4, PACKAGE_NAME); 633 OverrideAllowedState stateDisabledChange = 634 overrideValidator.getOverrideAllowedState(5, PACKAGE_NAME); 635 OverrideAllowedState stateDLoggingOnlyChange = 636 overrideValidator.getOverrideAllowedState(6, PACKAGE_NAME); 637 638 assertThat(stateTargetSdkLessChange) 639 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 640 assertThat(stateTargetSdkEqualChange) 641 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 642 assertThat(stateTargetSdkAfterChange) 643 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 644 assertThat(stateEnabledChange) 645 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 646 assertThat(stateDisabledChange) 647 .isEqualTo(new OverrideAllowedState(DEFERRED_VERIFICATION, -1, -1)); 648 assertThat(stateDLoggingOnlyChange) 649 .isEqualTo(new OverrideAllowedState(LOGGING_ONLY_CHANGE, -1, -1)); 650 } 651 } 652