1 /* 2 * Copyright (C) 2021 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.modules.updatablesharedlibs; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assume.assumeTrue; 21 import static org.junit.Assert.assertThrows; 22 23 import com.android.tradefed.targetprep.TargetSetupError; 24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 26 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; 27 import com.android.internal.util.test.SystemPreparer; 28 29 import com.android.modules.utils.build.testing.DeviceSdkLevel; 30 31 import org.junit.Assert; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.rules.RuleChain; 35 import org.junit.rules.TemporaryFolder; 36 import org.junit.runner.RunWith; 37 38 import android.cts.install.lib.host.InstallUtilsHost; 39 40 @RunWith(DeviceJUnit4ClassRunner.class) 41 public class UpdatableSharedLibsTest extends BaseHostJUnit4Test { 42 43 private final InstallUtilsHost mHostUtils = new InstallUtilsHost(this); 44 private final TemporaryFolder mTemporaryFolder = new TemporaryFolder(); 45 private final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder, this::getDevice); 46 47 @Rule 48 public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer); 49 50 @Test callOnDeviceApiFromHost()51 public void callOnDeviceApiFromHost() throws Exception { 52 if (!getDevice().isAdbRoot()) { 53 getDevice().enableAdbRoot(); 54 } 55 assumeTrue("Device needs to run on at least T", isAtLeastT()); 56 assumeTrue("Device does not support updating APEX", mHostUtils.isApexUpdateSupported()); 57 assumeTrue("Device requires root", getDevice().isAdbRoot()); 58 59 // install app requiring lib before the lib is installed 60 assertInstalationFails(); 61 62 String apex = "test_com.android.modules.updatablesharedlibs.apex"; 63 mPreparer.pushResourceFile(apex, "/system/apex/" + apex); 64 mPreparer.reboot(); 65 getDevice().disableAdbRoot(); 66 67 installPackage("com.android.modules.updatablesharedlibs.apps.targetS.apk"); 68 installPackage("com.android.modules.updatablesharedlibs.apps.targetT.apk"); 69 installPackage("com.android.modules.updatablesharedlibs.apps.targetTWithLib.apk"); 70 71 runDeviceTests("com.android.modules.updatablesharedlibs.apps.targetS", null); 72 runDeviceTests("com.android.modules.updatablesharedlibs.apps.targetT", null); 73 runDeviceTests("com.android.modules.updatablesharedlibs.apps.targetTWithLib", null); 74 } 75 assertInstalationFails()76 private void assertInstalationFails() throws Exception { 77 String packageName = "com.android.modules.updatablesharedlibs.apps.targetTWithLib"; 78 Exception e = assertThrows( 79 TargetSetupError.class, 80 () -> installPackage(packageName + ".apk")); 81 assertThat(e).hasMessageThat().contains( 82 "Package " + packageName + " requires " 83 + "unavailable shared library " 84 + "com.android.modules.updatablesharedlibs.libs.since.t"); 85 } 86 isAtLeastT()87 protected boolean isAtLeastT() throws Exception { 88 DeviceSdkLevel deviceSdkLevel = new DeviceSdkLevel(getDevice()); 89 return deviceSdkLevel.isDeviceAtLeastT(); 90 } 91 } 92