1 /* 2 * Copyright (C) 2024 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.tests.sdksandbox.restrictions.host; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assume.assumeTrue; 22 23 import android.app.sdksandbox.hosttestutils.AconfigFlagUtils; 24 import android.app.sdksandbox.hosttestutils.SdkSandboxDeviceSupportedHostRule; 25 26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 28 29 import com.google.common.truth.Expect; 30 31 import org.junit.Rule; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @RunWith(DeviceJUnit4ClassRunner.class) 36 public class SdkSandboxRestrictionsHostTest extends BaseHostJUnit4Test { 37 private static final String TEST_APP_RESTRICTIONS_PACKAGE = 38 "com.android.tests.sdksandbox.restrictions"; 39 private static final String TEST_APP_RESTRICTIONS_APK = "SdkSandboxRestrictionsTestApp.apk"; 40 private final AconfigFlagUtils mAconfigFlagUtils = new AconfigFlagUtils(this); 41 42 @Rule(order = 0) 43 public final SdkSandboxDeviceSupportedHostRule deviceSupportRule = 44 new SdkSandboxDeviceSupportedHostRule(this); 45 46 @Rule(order = 1) 47 public final Expect expect = Expect.create(); 48 49 /** 50 * Runs the given phase of a test by calling into the device. Throws an exception if the test 51 * phase fails. 52 * 53 * <p>For example, <code>runPhase("testExample");</code> 54 */ runPhase(String phase)55 private void runPhase(String phase) throws Exception { 56 assertThat( 57 runDeviceTests( 58 TEST_APP_RESTRICTIONS_PACKAGE, 59 TEST_APP_RESTRICTIONS_PACKAGE 60 + "." 61 + "SdkSandboxRestrictionsTestApp", 62 phase)) 63 .isTrue(); 64 } 65 66 @Test testInvalidateETSV()67 public void testInvalidateETSV() throws Exception { 68 assumeTrue( 69 mAconfigFlagUtils.isFlagEnabled( 70 "com.android.adservices.flags." 71 + "sdksandbox_dump_effective_target_sdk_version")); 72 assumeTrue( 73 mAconfigFlagUtils.isFlagEnabled( 74 "com.android.adservices.flags." 75 + "sdksandbox_use_effective_target_sdk_version_for_restrictions")); 76 77 installPackage(TEST_APP_RESTRICTIONS_APK); 78 79 String currentUserId = getDevice().executeShellCommand("am get-current-user").trim(); 80 String uid = 81 getDevice() 82 .executeShellCommand( 83 "pm list packages -U --user " 84 + currentUserId 85 + " " 86 + TEST_APP_RESTRICTIONS_PACKAGE) 87 .split(":")[2] 88 .trim(); 89 90 runPhase("populateETSV"); 91 92 String processDump = getDevice().executeShellCommand("dumpsys sdk_sandbox"); 93 expect.that(processDump).contains(uid + ": 34"); 94 95 installPackage(TEST_APP_RESTRICTIONS_APK); 96 processDump = getDevice().executeShellCommand("dumpsys sdk_sandbox"); 97 expect.that(processDump).doesNotContain(uid + ": 34"); 98 } 99 } 100