1 /* 2 * Copyright (C) 2018 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 package com.android.server.om.hosttest; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 24 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 @RunWith(DeviceJUnit4ClassRunner.class) 32 public class InstallOverlayTests extends BaseHostJUnit4Test { 33 private static final String SIG_OVERLAY_PACKAGE_NAME = 34 "com.android.server.om.hosttest.signature_overlay"; 35 private static final String APP_OVERLAY_PACKAGE_NAME = 36 "com.android.server.om.hosttest.app_overlay"; 37 private static final String FRAMEWORK_OVERLAY_PACKAGE_NAME = 38 "com.android.server.om.hosttest.framework_overlay"; 39 private static final String[] ALL_PACKAGES = new String[] { 40 SIG_OVERLAY_PACKAGE_NAME, APP_OVERLAY_PACKAGE_NAME, FRAMEWORK_OVERLAY_PACKAGE_NAME 41 }; 42 43 private static final String DEVICE_TEST_PKG = 44 "com.android.server.om.hosttest.update_overlay_test"; 45 private static final String DEVICE_TEST_CLS = DEVICE_TEST_PKG + ".UpdateOverlayTest"; 46 47 @Before ensureNoOverlays()48 public void ensureNoOverlays() throws Exception { 49 // Make sure we're starting with a clean slate. 50 for (String pkg : ALL_PACKAGES) { 51 assertFalse(pkg + " should not be installed", isPackageInstalled(pkg)); 52 assertFalse(pkg + " should not be registered with overlay manager service", 53 overlayManagerContainsPackage(pkg)); 54 } 55 } 56 57 /* 58 For some reason, SuiteApkInstaller is *not* uninstalling overlays, even though #installPackage() 59 claims it will auto-clean. 60 TODO(b/72877546): Remove when auto-clean is fixed. 61 */ 62 @After uninstallOverlays()63 public void uninstallOverlays() throws Exception { 64 for (String pkg : ALL_PACKAGES) { 65 uninstallPackage(pkg); 66 } 67 } 68 69 @Test failToInstallNonPlatformSignedOverlayTargetPreQ()70 public void failToInstallNonPlatformSignedOverlayTargetPreQ() throws Exception { 71 try { 72 installPackage("OverlayHostTests_NonPlatformSignatureOverlay.apk"); 73 fail("installed a non-platform signed overlay with targetSdkVersion < Q"); 74 } catch (Exception e) { 75 // Expected. 76 } 77 assertFalse(overlayManagerContainsPackage(SIG_OVERLAY_PACKAGE_NAME)); 78 } 79 80 @Test failToInstallPlatformSignedStaticOverlay()81 public void failToInstallPlatformSignedStaticOverlay() throws Exception { 82 try { 83 installPackage("OverlayHostTests_PlatformSignatureStaticOverlay.apk"); 84 fail("installed a static overlay"); 85 } catch (Exception e) { 86 // Expected. 87 } 88 assertFalse(overlayManagerContainsPackage(SIG_OVERLAY_PACKAGE_NAME)); 89 } 90 91 @Test installPlatformSignedOverlay()92 public void installPlatformSignedOverlay() throws Exception { 93 installPackage("OverlayHostTests_PlatformSignatureOverlay.apk"); 94 assertTrue(overlayManagerContainsPackage(SIG_OVERLAY_PACKAGE_NAME)); 95 } 96 97 @Test installPlatformSignedAppOverlayAndUpdate()98 public void installPlatformSignedAppOverlayAndUpdate() throws Exception { 99 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, "expectAppResource")); 100 101 installPackage("OverlayHostTests_AppOverlayV1.apk"); 102 setOverlayEnabled(APP_OVERLAY_PACKAGE_NAME, true); 103 assertTrue(overlayManagerContainsPackage(APP_OVERLAY_PACKAGE_NAME)); 104 assertEquals("v1", getDevice() 105 .getAppPackageInfo(APP_OVERLAY_PACKAGE_NAME) 106 .getVersionName()); 107 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, 108 "expectAppOverlayV1Resource")); 109 110 installPackage("OverlayHostTests_AppOverlayV2.apk"); 111 assertTrue(overlayManagerContainsPackage(APP_OVERLAY_PACKAGE_NAME)); 112 assertEquals("v2", getDevice() 113 .getAppPackageInfo(APP_OVERLAY_PACKAGE_NAME) 114 .getVersionName()); 115 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, 116 "expectAppOverlayV2Resource")); 117 } 118 119 @Test installPlatformSignedFrameworkOverlayAndUpdate()120 public void installPlatformSignedFrameworkOverlayAndUpdate() throws Exception { 121 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, "expectFrameworkResource")); 122 123 installPackage("OverlayHostTests_FrameworkOverlayV1.apk"); 124 setOverlayEnabled(FRAMEWORK_OVERLAY_PACKAGE_NAME, true); 125 assertTrue(overlayManagerContainsPackage(FRAMEWORK_OVERLAY_PACKAGE_NAME)); 126 assertEquals("v1", getDevice() 127 .getAppPackageInfo(FRAMEWORK_OVERLAY_PACKAGE_NAME) 128 .getVersionName()); 129 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, 130 "expectFrameworkOverlayV1Resource")); 131 132 installPackage("OverlayHostTests_FrameworkOverlayV2.apk"); 133 assertTrue(overlayManagerContainsPackage(FRAMEWORK_OVERLAY_PACKAGE_NAME)); 134 assertEquals("v2", getDevice() 135 .getAppPackageInfo(FRAMEWORK_OVERLAY_PACKAGE_NAME) 136 .getVersionName()); 137 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, 138 "expectFrameworkOverlayV2Resource")); 139 } 140 141 @Test enabledFrameworkOverlayMustAffectNewlyInstalledPackage()142 public void enabledFrameworkOverlayMustAffectNewlyInstalledPackage() throws Exception { 143 try { 144 setPackageEnabled(DEVICE_TEST_PKG, false); 145 146 installPackage("OverlayHostTests_FrameworkOverlayV1.apk"); 147 setOverlayEnabled(FRAMEWORK_OVERLAY_PACKAGE_NAME, true); 148 assertTrue(overlayManagerContainsPackage(FRAMEWORK_OVERLAY_PACKAGE_NAME)); 149 150 setPackageEnabled(DEVICE_TEST_PKG, true); 151 assertTrue(runDeviceTests(DEVICE_TEST_PKG, DEVICE_TEST_CLS, 152 "expectFrameworkOverlayV1Resource")); 153 } finally { 154 setPackageEnabled(DEVICE_TEST_PKG, true); 155 } 156 } 157 158 @Test instantAppsNotVisibleToOMS()159 public void instantAppsNotVisibleToOMS() throws Exception { 160 installInstantPackage("OverlayHostTests_AppOverlayV1.apk"); 161 assertFalse(overlayManagerContainsPackage(APP_OVERLAY_PACKAGE_NAME)); 162 installConvertExistingInstantPackageToFull(APP_OVERLAY_PACKAGE_NAME); 163 assertTrue(overlayManagerContainsPackage(APP_OVERLAY_PACKAGE_NAME)); 164 } 165 166 @Test changesPersistedWhenUninstallingDisabledOverlay()167 public void changesPersistedWhenUninstallingDisabledOverlay() throws Exception { 168 getDevice().enableAdbRoot(); 169 assertFalse(getDevice().executeShellCommand("cat /data/system/overlays.xml") 170 .contains(APP_OVERLAY_PACKAGE_NAME)); 171 installPackage("OverlayHostTests_AppOverlayV1.apk"); 172 assertTrue(getDevice().executeShellCommand("cat /data/system/overlays.xml") 173 .contains(APP_OVERLAY_PACKAGE_NAME)); 174 uninstallPackage(APP_OVERLAY_PACKAGE_NAME); 175 delay(); 176 assertFalse(getDevice().executeShellCommand("cat /data/system/overlays.xml") 177 .contains(APP_OVERLAY_PACKAGE_NAME)); 178 } 179 180 @Test testAdbShellOMSInterface()181 public void testAdbShellOMSInterface() throws Exception { 182 installPackage("OverlayHostTests_AppOverlayV1.apk"); 183 assertTrue(shell("cmd overlay list " + DEVICE_TEST_PKG).contains(DEVICE_TEST_PKG)); 184 assertTrue(shell("cmd overlay list " + DEVICE_TEST_PKG).contains(APP_OVERLAY_PACKAGE_NAME)); 185 assertEquals("[ ] " + APP_OVERLAY_PACKAGE_NAME, 186 shell("cmd overlay list " + APP_OVERLAY_PACKAGE_NAME).trim()); 187 assertEquals("STATE_DISABLED", 188 shell("cmd overlay dump state " + APP_OVERLAY_PACKAGE_NAME).trim()); 189 190 setOverlayEnabled(APP_OVERLAY_PACKAGE_NAME, true); 191 assertEquals("[x] " + APP_OVERLAY_PACKAGE_NAME, 192 shell("cmd overlay list " + APP_OVERLAY_PACKAGE_NAME).trim()); 193 assertEquals("STATE_ENABLED", 194 shell("cmd overlay dump state " + APP_OVERLAY_PACKAGE_NAME).trim()); 195 } 196 delay()197 private void delay() { 198 try { 199 Thread.sleep(1000); 200 } catch (InterruptedException e) { 201 } 202 } 203 installPackage(String pkg)204 private void installPackage(String pkg) throws Exception { 205 super.installPackage(pkg); 206 delay(); 207 } 208 installInstantPackage(String pkg)209 private void installInstantPackage(String pkg) throws Exception { 210 super.installPackage(pkg, "--instant"); 211 delay(); 212 } 213 installConvertExistingInstantPackageToFull(String pkg)214 private void installConvertExistingInstantPackageToFull(String pkg) throws Exception { 215 shell("cmd package install-existing --wait --full " + pkg); 216 } 217 setPackageEnabled(String pkg, boolean enabled)218 private void setPackageEnabled(String pkg, boolean enabled) throws Exception { 219 shell("cmd package " + (enabled ? "enable " : "disable ") + pkg); 220 delay(); 221 } 222 setOverlayEnabled(String pkg, boolean enabled)223 private void setOverlayEnabled(String pkg, boolean enabled) throws Exception { 224 shell("cmd overlay " + (enabled ? "enable " : "disable ") + pkg); 225 delay(); 226 } 227 overlayManagerContainsPackage(String pkg)228 private boolean overlayManagerContainsPackage(String pkg) throws Exception { 229 return shell("cmd overlay list").contains(pkg); 230 } 231 shell(final String cmd)232 private String shell(final String cmd) throws Exception { 233 return getDevice().executeShellCommand(cmd); 234 } 235 } 236