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 17 package android.backup.cts; 18 19 import static androidx.test.InstrumentationRegistry.getInstrumentation; 20 21 import static org.testng.Assert.expectThrows; 22 23 import android.content.Context; 24 import android.content.pm.ApplicationInfo; 25 import android.os.IBinder; 26 import android.platform.test.annotations.AppModeFull; 27 28 import java.lang.reflect.Method; 29 30 @AppModeFull 31 public class AgentBindingTest extends BaseBackupCtsTest { 32 private Context mContext; 33 34 @Override setUp()35 protected void setUp() throws Exception { 36 super.setUp(); 37 mContext = getInstrumentation().getTargetContext(); 38 } 39 testUnbindBackupAgent_isNotCallableFromCts()40 public void testUnbindBackupAgent_isNotCallableFromCts() throws Exception { 41 if (!isBackupSupported()) { 42 return; 43 } 44 expectThrows(Exception.class, () -> unbindBackupAgent(mContext.getApplicationInfo())); 45 } 46 testBindBackupAgent_isNotCallableFromCts()47 public void testBindBackupAgent_isNotCallableFromCts() throws Exception { 48 if (!isBackupSupported()) { 49 return; 50 } 51 expectThrows(Exception.class, () -> bindBackupAgent(mContext.getPackageName(), 0, 0)); 52 } 53 unbindBackupAgent(ApplicationInfo applicationInfo)54 private static void unbindBackupAgent(ApplicationInfo applicationInfo) throws Exception { 55 callActivityManagerMethod( 56 "unbindBackupAgent", 57 new Class<?>[] {ApplicationInfo.class}, 58 new Object[] {applicationInfo}); 59 } 60 bindBackupAgent(String packageName, int backupRestoreMode, int userId)61 private static void bindBackupAgent(String packageName, int backupRestoreMode, int userId) 62 throws Exception { 63 callActivityManagerMethod( 64 "bindBackupAgent", 65 new Class<?>[] {String.class, int.class, int.class}, 66 new Object[] {packageName, backupRestoreMode, userId}); 67 } 68 callActivityManagerMethod( String methodName, Class<?>[] types, Object[] args)69 private static void callActivityManagerMethod( 70 String methodName, Class<?>[] types, Object[] args) throws Exception { 71 Class<?> activityManagerClass = Class.forName("android.app.IActivityManager"); 72 Object activityManager = getActivityManager(); 73 Method bindBackupAgentMethod = activityManagerClass.getMethod(methodName, types); 74 bindBackupAgentMethod.invoke(activityManager, args); 75 } 76 getActivityManager()77 private static Object getActivityManager() throws Exception { 78 Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager"); 79 Class<?> stubClass = Class.forName("android.app.IActivityManager$Stub"); 80 Method asInterfaceMethod = stubClass.getMethod("asInterface", IBinder.class); 81 Method getServiceMethod = serviceManagerClass.getMethod("getService", String.class); 82 return asInterfaceMethod.invoke( 83 null, (IBinder) getServiceMethod.invoke(serviceManagerClass, "activity")); 84 } 85 } 86